- 20 March 2019
- WordPress (25)
This can occur when you use taxonomies in your permalinks, e.g. if your permalink structure looks something like:
/%category%/%postname%/
Here’s a guide on how to resolve this issue by Mark Butler from Bamboo Manchester:
How to Fix 404 Errors in WordPress Pagination
I’ve extended this slightly below to exclude certain pages, which you could adjust however you like. In the example below, the two functions will run for everything except the page with the slug ‘news’.
//fix 404 errors with pagination caused by custom permalink structure //https://www.bamboomanchester.uk/fix-404-errors-wordpress-pagination/ //remove the offending parts of the url before WP tries to process it function bamboo_request($query_string ){ //dont run if page is news if (is_page('news')) { return; } 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'); //append page numbers from url to the query function bamboo_pre_get_posts( $query ) { //dont run if page is news if (is_page('news')) { return; } if( $query->is_main_query() && !$query->is_feed() && !is_admin() ) { $query->set( 'paged', str_replace( '/', '', get_query_var( 'page' ) ) ); } } add_action('pre_get_posts','bamboo_pre_get_posts');
the is_page function supports arrays too, so if you want to exclude multiple slugs just adjust your functions like so:
if (is_page( array('news','blog')) ) { return; }
Comments
Whether you have feedback, a question, want to share your opinion, or simply want to say thank you - we welcome comments! Please read the disclaimer.