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.