WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

                  $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";

                            //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";

share|improve this question
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 );
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

Your Answer


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.