List Posts By Custom Taxonomy

When you change URLs and post types/taxonomies, you need to flush and regenerate permalinks/rewrite rules to make the changes take effect. If you don’t, you’ll get 404 errors. You can do this by visiting the permalinks settings page.

Your term will then be viewable at:

/genre/fiction

And take the template taxonomy-genre.php or taxonomy.php or archive.php in that order.

You can change these URLs using the rewrite parameter when registering a post type or taxonomy. Just be careful of rule clashes.

For example:

register_taxonomy(
    'genre',
    'book',
    array(
        'label' => __( 'Genre' ),
        'rewrite' => array( 'slug' => 'books' ), // slug is now books, not genre
    )
);

This will give you URLs such as:

/books/fiction

Where fiction is the slug of the fiction term. Note that if the post type book has a slug of books, this may not work, as there is now 2 sets of rewrite rules for the same URL that do different things, and one will replace the other, leading to either broken book URLs or broken Genre listing URLs.

However, you wanted URLs of the format: /book/xxxx. You cannot have this URL format for books and genres. You need to make a choice and change either the taxonomy, or the post type to use a different rewrite URL. Otherwise you will have clashing rewrite rules.

For example, if you added a book called fiction, and visited this URL:

/book/fiction

What would you get? Would you get the book called fiction? Or would you get the fiction genre listings? It depends on which you registered first and the priority of the rewrite rule.

You would either get a fiction listing with a fiction book that you could never reach, or you would have a fiction section that could never be reached because it would show the fiction book instead of the fiction listings.

What if you had a fiction genre, and a apocalypse book? Then one of the URLs would 404. You would have one of these scenarios depending on the order of the rewrite rules:

  • WordPress searches for a genre called apocalypse and fails to find it, giving a 404
  • WordPress searches for a book called fiction and fails to find it, giving a 404