Aschab Aschab - 5 months ago 38
PHP Question

Allow pagination in single custom taxonomy in Wordpress

I registered a custom taxonomy 'genre' and created a custom template taxonomy-genre.php

the link:

website.com/genre/comedy

works perfectly but

website.com/genre/comedy/page/2

gives me 404 error. I dump $wp_query and I get:

public 'query' =>
array (size=2)
'paged' => string '2' (length=1)
'genre' => string 'comedy' (length=6)


So the query is correctly understanding that we're looking for the taxonomy genre "comedy" in page 2, but for some reason is not loading the correct template.

Answer Source

As everything is working except for the pagination, it sounds like you don't have enough posts with that category to have a page 2.

With any pagination in Wordpress (not just for custom taxonomies), you will get a 404 error if you try to access a page number that doesn't exist - it's the same as accessing any page that doesn't exist.

Example: if you have 15 uncategorised posts and show 10 posts per page (i.e. enough for 2 pages of posts), you will get the following:

  • website.com/category/uncategorized/ - shows posts 1-10
  • website.com/category/uncategorized/page/2/ - shows posts 11-15
  • website.com/category/uncategorized/page/3/ - 404 error: There are not enough posts for a third page

(Apologies for the simplistic example! I'm sure you understood, but I thought I'd include it for others with less experience who may have the same problem)