I have two custom post types, type1 and type2. Type2 posts have a postmeta called postID that is the ID number of a post in type1. When showing type1 in single.php I want a mysql query that selects the type2 post.

The only data I have is the ID number of type1. So I have to select from type2 postmeta where the meta_key = postID and the meta_value = type1 ID. I then need to use the post_id of that row to select the post from wp_posts.

I'm not good with forming mysql queries, so is there a single query that will do this?

1 Answer 1


If I understand the question you are trying to show both post on single.php something like this should work as a second loop.

Put This INSIDE the first loop. Do this so you can get the current post ID


// Second Loop
$query = new WP_Query( array ( 'post_type' => 'TYPE1', 'meta_key' => $parent_id ) );

while ( $query->have_posts() ) : $query->the_post();
//normal loop stuff such as the_content();
    echo '<li>';
    echo '</li>';

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.