Get the specific value of the_meta() in WordPress?

10,549

Solution 1

Try using get_post_meta() in place of the meta. It will return the meta value for a specific page/post.

For more info visit at http://codex.wordpress.org/Function_Reference/get_post_meta

Solution 2

In loop you can use

<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
   <p><?php echo post_custom( 'my_meta_key' ) ; ?></p>
<?php  endwhile; endif; ?>
Share:
10,549
Raja
Author by

Raja

Updated on June 05, 2022

Comments

  • Raja
    Raja almost 2 years

    I'm trying exhaustively to get a specific row from within a loop using the_meta() array in Wordpress and assigning a variable to it (like the procedural while loop). Right now it simply pulls all of the rows at once. The result from this query returns 3 rows from the database. What I am trying to do it wrap a div tag around one of the returned rows that is stored within the_meta();.

    I've tried exploding the array and returning the first row but it just returns everything all at once. I just want to get a row and put it in a variable so that I can style it using CSS.

    Here is the code:

    $args = array( 
    'post_type' => 'membersprofile', 
    'posts_per_page' => 20);        
    
    $loop = new WP_Query( $args );
    
        while ( $loop->have_posts() ) : $loop->the_post();  
    
    $newvar = explode(" ", the_meta());
    echo $newvar[0];
    
    endwhile;
    

    Any help would be greatly appreciated!

    EDIT: Thanks to Youn for pointing me in the right direction and finding the answer for me, The problem was I was using the_meta() which only returned the whole rows. By using get_post_meta I was able to assign variables to each row returned. The code that works is:

    $key_2_value = get_post_meta(get_the_ID(), 'wpcf-shortbio', true);
    
    // check if the custom field has a value
    if($key_2_value != '') {
        echo $key_2_value;
    }  
    

    Hope this helps someone else!