I have two custom post types: books and authors. Both of them have attached a custom taxonomy - genres.

How would I display the books in the fiction genre?

What would be the URL used and the name of the template?

I can think of two methods to do this, but they have disadvantages, so I'm looking for a method that that would be similar with WP template hierarchy: visiting site.com/genre/fiction/ would call this template taxonomy-genre.php

Method 1: using custom page templates I would create a custom page template, cpt-genre.php. Then, I would create an empty page for each genre in books, using slugs like books-fiction and so on. In the template, I'd read the slug of the page, interpret it and query to get the books in fiction genre.
Result: accessing site.com/books-fiction would get to cpt-genre.php, and the template would display the data based on the slug of the page.
Downside: difficult to administer

Method 2: add a query string to the the taxonomy URL, read it in taxonomy-books.php and taxonomy-authors.php and modify WP_Query to display only the books / authors in the mentioned genre.
Result: accessing site.com/books/?genre=fiction would get to taxonomy-books.php, and the template would display the data based on the query string.
Downside: the URL is not clean and, also, it's not SEO friendly

So, any other ideas for new methods or, at least, for ways to mitigate the downsides of the above methods?


2 Answers 2


I think this is what you are searching for... You must use both post_type and tax_query to filter your query

 $query = new WP_Query( array(
    'post_type' => 'books',          // name of post type.
    'tax_query' => array(
            'taxonomy' => 'fiction',   // taxonomy name
) );

while ( $query->have_posts() ) : $query->the_post();
    // do stuff here....

 * reset the orignal query
 * we should use this to reset wp_query
  • Great code, it would work on a custom page template, for instance, but nothing similar to using taxonomy-genre.php to display site.com/genre/fiction. I am amending my initial question with this clarification. Commented Jul 27, 2017 at 15:47

Similarly, do you need to display just the authors per genre?

If not, then you can hook on pre_get_posts and just remove the author post type from the query's post types (or re-set the whole thing to books) when is_tax( 'genre' ) is true.

  • Yes, I also need a page for the authors in a genre. Your solution would be great if I did not need that. Commented Jul 27, 2017 at 21:50

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.