0

I'm working on a category page. I want to show all the posts that belong to that category on one page. Each post makes use of a custom field that I have added. I want to loop through the posts to display them after each other, & show the custom field value that belongs to each post as well.

How do I loop through the custom fields to get the value that belongs to each post? I want to display this value next to the rest of each post. I am working with a plugin called advanced events manager.

My code to get the value of the custom field for each post so far, that doesn't work:

$latest_cpt = get_posts("post_type=event"); 
$theidone =$latest_cpt[0]->ID;
$this_post_id =  $theidone; //get_the_ID();
$key_2_value =  get_post_meta( $this_post_id, 'custom_select', true );
if( ! empty( $key_2_value )) {
$thisisworking =   $key_2_value ;
 ;}
echo $thisisworking;

My code only get the custom field value for the first post, how can I get the custom field value for each post? Thanks!

0

You are missing "foreach" loop. Try this way

    $latest_cpt = get_posts("post_type=event"); 
foreach ( $latest_cpt as $cpt_post ) {    
$theidone =$cpt_post->ID;
    $this_post_id =  $theidone; //get_the_ID();
    $key_2_value =  get_post_meta( $this_post_id, 'custom_select', true );
    if( ! empty( $key_2_value )) {
    $thisisworking =   $key_2_value ;
     ;}
    echo $thisisworking; }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.