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


1 Answer 1


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 );
  • see edited code above, maybe I misunderstood what you're trying to do?
    – Milo
    Commented Nov 19, 2012 at 1:47
  • oh never mind, you figured it out!
    – Milo
    Commented Nov 19, 2012 at 1:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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