B a m b o o Menu Z

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!).

W