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 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
add comment

1 Answer

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

$parent_id=the_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>';
    the_title();
    echo '</li>';
endwhile;
share|improve this answer
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.