How resolve pagination bug when custom post type and page have same slugs?

When we register new post type is recommended use names in the singular (e.g. default post types — post, page, etc.), but in some cases it’s not always convenient. For example, we have post type News which has slug “news” and News page (but not archive page for news post type) which has same slug. If we want output our news posts on this page — no problem it will work. But if add pagination on this page and click on a link (https://site_name/page_name/page/2) of page we’ll get 404 error. And, interestingly, if we make pagination using ajax we can open page from pagination without 404 error, but if we directly open page https://site_name/page_name/page/2 then we’ll seen 404.

Resolve permalink conflict when a custom post type name and page have same slugs

Problem: Resolve pagination bug when a custom post type name and page have same slugs.

Solution: We should be get all custom post types, after then check is there page which has same slug and if yes overwrite permalink.

// Check URL Exists
function url_exists( $url ) {
	if ( ! $fp = curl_init( $url ) ) {
		return false;
	return true;
add_action( 'init', 'custom_rewrite_permalinks', 10 );
// Rewrite Links For Some Custom Post Types
function custom_rewrite_permalinks() {
	$post_types_args = array(
		'public'   => true,
		'_builtin' => false,
	$output = 'names'; // names or objects, note names is the default
	$operator = 'and'; // 'and' or 'or'
	$custom_post_types = get_post_types( $post_types_args, $output, $operator ); // Get only custom post types
	foreach ( $custom_post_types as $custom_post_type_slug ) {
		if ( url_exists( home_url( '/' . $custom_post_type_slug ) ) ) {
			add_rewrite_rule( '^' . $custom_post_type_slug . '/page/([0-9]+)', 'index.php?pagename=' . $custom_post_type_slug . '&paged=$matches[1]', 'top' );



Leave Comment

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