How to Fix 404 Errors in WordPress Pagination

You’re building out custom taxonomies in WordPress, everything’s looking tidy—and then… page 2 hits a wall. That wall being a good old-fashioned 404 error. If your permalinks use categories or taxonomies (e.g. /%category%/%postname%/), this is a surprisingly common issue. WordPress gets confused when pagination enters the mix.

In this guide, we’ll walk through what’s really going on, why it happens, and exactly how to fix it—either with a little bit of PHP or, if you’re in a rush, a simple plugin. Your pagination woes are fixable. And no, you’re definitely not the first to face them.

Taxonomies vs permalinks.

If you are creating custom taxonomies in WordPress, either as part of a theme or a plugin, you may encounter 404 errors when navigating those taxonomies on your website.

What on earth
is going on?

It generally only occurs if you are using the taxonomy within your permalinks, i.e. if your permalinks are set to something like this:

/%category%/%postname%/

At first glance everything will seem to be OK, until you navigate to page 2  of the archive page in question. At this point WordPress will send the browser to a URL looking something like this:

www.your_site.com/your_taxonomy/page/2

The appended ‘/page/2’ will conflict with the custom permalink setting, resulting in an a 404 error.

Don’t give me problems,
give me solutions.

We need to achieve two things to solve this problem. Firstly we need to remove the offending parts of the URL before WordPress tries to process the URL. Adding the following code to your functions.php (or as part of your initialisation code if you are a plugin author) will do just that:

function bamboo_request($query_string )
{
    if( isset( $query_string['page'] ) ) {
        if( ''!=$query_string['page'] ) {
            if( isset( $query_string['name'] ) ) {
                unset( $query_string['name'] );
            }
        }
    }
    return $query_string;
}
add_filter('request', 'bamboo_request');

Yeah but
what about the paging?

Assuming you would still like to have paging for your custom taxonomy, the follow code will take the page number from the URL and append it to the WordPress query in the required format. Again the follow code should be added to your functions.php or your plugin initialisation code:

add_action('pre_get_posts','bamboo_pre_get_posts');
function bamboo_pre_get_posts( $query ) { 
    if( $query->is_main_query() && !$query->is_feed() && !is_admin() ) { 
        $query->set( 'paged', str_replace( '/', '', get_query_var( 'page' ) ) ); 
    } 
}

That’s great but
do I really have to do all that?

If you don’t like the idea of writing all that code, or you’re just feeling a bit lazy, there is also a plugin available to fix this problem automatically available here: http://wordpress.org/support/view/plugin-reviews/category-pagination-fix (but where would the fun in that be!).

Mark Butler

Written by

Mark Butler

Hi, I’m Mark, the lead dev at Bamboo Manchester. 20 years of coding expertise crafting bespoke websites that are fast, flexible, and easy to use.