Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I have the following snippet of code, basically I get the set value of id from the metabox, get the current id based on the loop, try and match, if htey match I want to pull the values from the matched id, currently i can only get the id, but i would like to get the associated custom field values of that post as well.

Also I don't know why, even if it matches the value, it will still echo out the else statement twice, i guess cause it's in the while loops, but how do i get ride of that?

<?php 
                  $list_agent = get_post_meta( $post->ID,'ec_agents',true );    
                  $args = array( 'post_type' => 'agent_type');  

                    $the_query = new WP_Query( $args );
                    $id = $post->ID;
                    while ( $the_query->have_posts() ) : $the_query->the_post();

                        if ($id == $list_agent) {
                            echo "success";
                            the_title();

                            //Both Meta values not showing
                            echo get_post_meta( $post->ID,'_agent_phone',true );
                            echo get_post_meta( $post->ID,'_agent_email',true );
                        } else {
                            echo "better luck next time";
                        }
                    endwhile;

                    wp_reset_postdata();
                    ?>
share|improve this question
add comment

1 Answer

up vote 1 down vote accepted

You're setting $id = $post->ID; outside of the loop for agent_type posts, it'll never match.

Anyway, if you're not doing anything with the agent_type posts that don't match, just query for the one post you want by its ID:

$list_agent = get_post_meta( $post->ID,'ec_agents',true );
$args = array(
    'post_type' => 'agent_type',
    'p' => $list_agent
);
$agent_query = new WP_Query( $args );
while ( $agent_query->have_posts() ) : $agent_query->the_post();
    echo get_post_meta( get_the_ID(),'_agent_phone',true );
    echo get_post_meta( get_the_ID(),'_agent_email',true );
endwhile;
wp_reset_query();
share|improve this answer
    
Awesome thank you, works like a charm! –  Beto Nov 19 '12 at 1:40
    
see edited code above, maybe I misunderstood what you're trying to do? –  Milo Nov 19 '12 at 1:47
    
oh never mind, you figured it out! –  Milo Nov 19 '12 at 1:48
    
I did thanks for your help with this. –  Beto Nov 19 '12 at 2:30
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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