0

I have custom post type called books. This has a category taxonomy, called book_types. So, for example:

Books -> Fiction -> Da Vinci Code

  • Custom Post Type: Books
  • Custom Taxonomy: Book Types (fiction, non fiction, etc)
  • Single entry: Da Vinci Code

I have the permalinks structure to be:

/books/fiction#da-vinci-code 

All the books are listed in their taxonomy category page as shown above. Da Vinci Code is just one part of the page for Fiction... as seen from the #da-vinci-code hashed value in the URL.

This means the taxonomy template is the important one. So I have a template:

template-book_types.php 

But this doesn't work. WP gives a 404. What am I missing? My code to create the CPT and Taxonomy is below. (And yes, I have flushed the Permalinks many times.)

/**
 * Post Type: Books.
 */

$labels = [
    "name" => __( "Books", "custom-post-type-ui" ),
    "singular_name" => __( "Book", "custom-post-type-ui" ),
];

$args = [
    "label" => __( "Books", "custom-post-type-ui" ),
    "labels" => $labels,
    "public" => true,
    "publicly_queryable" => true,
    "show_ui" => true,
    "show_in_rest" => true,
    "rest_base" => "",
    "rest_controller_class" => "WP_REST_Posts_Controller",
    "has_archive" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "delete_with_user" => false,
    "exclude_from_search" => false,
    "capability_type" => "post",
    "map_meta_cap" => true,
    "hierarchical" => false,
    "can_export" => true,
    "rewrite" => [ "slug" => "books", "with_front" => true ],
    "query_var" => true,
    "menu_position" => 5,
    "menu_icon" => "dashicons-schedule",
    "supports" => [ "title", "editor" ],
    "taxonomies" => [ "book_types" ],
    "show_in_graphql" => false,
];

register_post_type( "books", $args );






/**
 * Taxonomy: Book Types.
 */

$labels = [
    "name" => __( "Book Types", "custom-post-type-ui" ),
    "singular_name" => __( "Book Type", "custom-post-type-ui" ),
];


$args = [
    "label" => __( "Book Types", "custom-post-type-ui" ),
    "labels" => $labels,
    "public" => true,
    "publicly_queryable" => true,
    "hierarchical" => true,
    "show_ui" => true,
    "show_in_menu" => true,
    "show_in_nav_menus" => true,
    "query_var" => true,
    "rewrite" => [ 'slug' => 'book_types', 'with_front' => true, ],
    "show_admin_column" => true,
    "show_in_rest" => true,
    "show_tagcloud" => false,
    "rest_base" => "book_types",
    "rest_controller_class" => "WP_REST_Terms_Controller",
    "show_in_quick_edit" => false,
    "sort" => false,
    "show_in_graphql" => false,
];
register_taxonomy( "book_types", [ "books" ], $args );
4
  • If you're actually referring to the post type archive, then the template name should be archive-books.php, but even so, /books/fiction would not show 404 error if there's a post with fiction as the slug, in your books CPT.
    – Sally CJ
    Apr 5 at 7:52
  • This is a bilingual (PolyLang) site. Funnily, the English works fine but the second language doesn't. There's no template with specifically English names in there. I'm first trying this with the overall templates like archive.php or single.php. In this case, why does it work with one language and not with the other?
    – Khom Nazid
    Apr 5 at 19:59
  • @SallyCJ in your example fiction won't be a post in books, it's the category (book_types taxonomy).
    – Khom Nazid
    Apr 6 at 2:14
  • Sorry, I can't help you with Polylang, and as for the "fiction" thing, I understand, but what I meant is, if there was a post (in your CPT) with the slug being fiction, then /books/fiction would have not shown a 404 page, because books is the rewrite base for your CPT. Also, if /book_types/fiction#da-vinci-code doesn't work, then try /en/book_types/fiction#da-vinci-code or whatever the correct language-based path set via Polylang. (and note that the /en/ isn't the "prefix" mentioned in my revised answer)
    – Sally CJ
    Apr 6 at 3:43

1 Answer 1

1

Your taxonomy's rewrite base is actually book_types (see the "rewrite" => [ 'slug' => 'book_types', 'with_front' => true, ] in your code), so the taxonomy archive pages are accessible at the /book_types/<term slug> path.. and not /books/<term slug>. So it's normal if /books/fiction shows a 404 error page, unless of course if there's a post with the slug fiction.

Secondly, note that 'with_front' => true means if your Custom Structure setting on the Permalink Settings admin page uses a prefix like /blog/ (with or without the leading/first slash), then your taxonomy's terms would also have that prefix in the permalink, e.g. https://example.com/blog/book_types/fiction/.

And the same also applies to post types where the with_front is set to true. So for example, https://example.com/blog/books/da-vinci-code/ would be the permalink for the "Da Vinci Code" post.

So as for the 404 error in question, try loading /book_types/fiction#da-vinci-code instead and check if you're seeing the correct taxonomy archive.

Also, you should know that the template name for a taxonomy archive should actually be in this form: taxonomy-<taxonomy>.php — or taxonomy-<taxonomy>-<term>.php for a specific term in a taxonomy, so you should rename your template from template-book_types.php to taxonomy-book_types.php so that WordPress loads it correctly on the taxonomy archive.

See https://developer.wordpress.org/themes/template-files-section/taxonomy-templates/ for more details.

3
  • Thanks. What should I change in my code to have /books/fiction#da-vinci-code as the URLs? I don't want the literal word book_types as a URL bit. (PS: The 'taxonomy-' prefix gives the same error.)
    – Khom Nazid
    Apr 5 at 18:27
  • Should I make the 'with_front' => false in the book_types if I don't tangibly want this specific phrase, but only the values of book_types in my URLs? I might have misunderstood what with_front meant.
    – Khom Nazid
    Apr 5 at 19:49
  • If you want the taxonomy and CPT to share the same rewrite base, i.e. books, then note that doing so will lead to a conflict, but you can post another question for more on that. And as for the 404 issue and regarding with_front, I have revised my answer, so check it out.
    – Sally CJ
    Apr 6 at 3:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.