0

Apologies if this is a stupid question, I've had a google and a look on the site but can't find an answer to my question.

I'm working on a project and have created a custom post type called 'books'. I've also created a taxonomy called 'genre', which has a term 'fiction.

How do you show all posts of taxonomy type 'fiction'... i.e.

http://www.example.com/books/fiction/

I can get /books/the-book-title/ as standard, but how do I get a listing page to work?

1 Answer 1

2

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
8
  • Thanks for clarifying, and that works. Is there a way to make the URL /books/fiction?
    – Sjwdavies
    Commented Feb 7, 2014 at 13:03
  • try setting your book post type permalink to 'book' and the taxonomy permalink to 'books' via the rewrite parameter, see my updated answer with a brief example
    – Tom J Nowell
    Commented Feb 7, 2014 at 13:42
  • No joy - I can't get that to work at all
    – Sjwdavies
    Commented Feb 7, 2014 at 14:28
  • Can you post your code, and have you flushed permalinks? Remember, if you try to have books for both the taxonomy and the post type, there will be clashes and things will break. Right now without seeing your code to register the post type or taxonomy I can only second guess you
    – Tom J Nowell
    Commented Feb 7, 2014 at 14:59
  • I just get a 404 page when I try /books/fiction/ (and yes i've flushed the permalinks numerous times)
    – Sjwdavies
    Commented Feb 7, 2014 at 15:03

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.