Pagination links redirect to homepage on WordPress

When using pagination on homepage for custom post types, pagination links appear with proper order, but clicking on pagination links will make the user to redirect to homepage. Due to wordpress core redirect_canonical function handling pagination links on homepage problem. To avoid this we need to add filter function to stop this problem. Here is the code to stop such pagination redirection issues.

Add below code in functions.php at end of the file.

add_filter('redirect_canonical','disable_frontpage_pagination_redirect_canonical', 10, 2);
function disable_frontpage_pagination_redirect_canonical($redirect_url, $requested_url) {
	if( is_front_page() && preg_match('@/page/@i', $requested_url)) {
		$redirect_url = false;
	}
	return $redirect_url;
}

Above code will check whether front page and have pagination in links, then it will stop redirection by setting false.

Leave a Reply

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

*