Fixing 404 Errors with WordPress Pagination


Categories
Tags
When using a custom permalink structure in WordPress, you may encounter 404 errors when using pagination.

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.

Disclaimer and Legal Information

The use of any content found on or via this website (code, how to guides etc.) is done so at your own risk. We take no responsibility for any issues encountered.

This blog and its authors will not be held responsible for any misuse, reuse, recycled and cited/uncited copies of content from this website by others.

The views and opinions expressed in this blog are those of the author(s) and do not necessarily reflect the position or opinion of any other agency, organisation, employer or company.

Comments are moderated but we do not take any responsibility for any libel or litigation that results from something written in a comment. We reserve the right to reject or delete any comment for any reason whatsoever (abusive, profane, rude etc). Please keep your comments polite and relevant.

We are happy for you to quote and share our content in any reasonable manner, e.g. post links to our blogs on social media, but not in any way that suggests that we, or our authors, endorse you, your use or your views. Nothing negative please.

We appreciate attributions, e.g. a link to our website (www.dragoncode.co.uk).

Thank you – we hope you find the website useful.