Taxonomy Archive Pagination 404

The issue:
When creating a custom taxonomy archive and you want to modify the posts_per_page setting. The first page works just fine, but subsequent pages will take into account the blog’s posts per page setting. This means that while the first page will show the number of taxonomy terms specified by the “posts_per_page” query_posts() variable, subsequent pages may return a 404 error or just mess up your pagination. This may only be an issue when using a page template such as taxonomy-{$taxonomy}.php

Why this occurs:
Specifically, I’ve no idea. Generally this is because the WordPress query is determining what template to show before it ever gets to the page template. The query takes the number of taxonomy terms to show for a given taxonomy, looks at the blog option posts_per_page and then determines what template to serve (404 or your custom one).

How to fix:
The solution I found was to filter the blog option posts_per_page if viewing the custom taxonomy. It’s pretty simple and looks like this:

[php]
add_filter( ‘option_posts_per_page’, ‘tdd_tax_filter_posts_per_page’ );
function tdd_tax_filter_posts_per_page( $value ) {
return (is_tax(‘custom-taxonomy’)) ? 1 : $value;
}
[/php]

The filter attaches to the posts_per_page filter hook. It then returns a “1” for one post per page if it’s the taxonomy archive I’m concerned about, otherwise it returns the default value.

3 thoughts on “Taxonomy Archive Pagination 404

  1. Thank you very much!!
    A simple solution and it just saved my day.
    I really got a headache trying to solve this issue for almost a day!

Leave a Reply

Your email address will not be published. Required fields are marked *