1

I have 2 custom post types: "Services" and "Works" and they both have a same taxonomy 'Genre'. Now the taxonomy 'Genre' has as term called 'novice'. 5 Recent posts under "Works" which has the term 'novice' (under taxonomy 'Genre') has to be queried on the post pages of "Services" which has the term 'novice' (under taxonomy 'Genre'). There will be 50 terms under the taxonomy.

1 Answer 1

1

Assuming you want it for all the terms in the 'Genre' taxonomy. In the loop on the single Service post page, put this code:

<?php
$the_terms = get_the_terms( get_the_ID, 'genre' );
if(isset($the_terms) && !empty($the_terms)){
    foreach($the_terms as $the_term){
        $the_terms_slugs[] = $the_term->slug;
    }
}

$works = get_posts(array(
                                'post_type' => 'works',
                                'posts_per_page' => 5,
                                'tax_query' => array(array(
                                    'taxonomy' => 'genre',
                                    'field' => 'slug',
                        'terms' => $the_terms_slugs
                                )));

//This will print the works which have the same genre as the current post
print_r($works_query);
?>

If you want it only for 'novice', let me know and I'll modify the code.

2
  • What I want to do is that in the single-service page it should query the 5 recent posts of the same taxonomy term what the selected service page has but the post type will be different. In this case it should query 5 recent posts of the term 'novice' from the 'Works' post type. I hope the above does the same. Commented Jun 28, 2012 at 19:59
  • 1
    Yes! Thats what my code above does... try it. Commented Jun 29, 2012 at 5:57

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.