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 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?

share|improve this question

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>';
share|improve this answer

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.