Wordpress - Resolve a custom post type name vs. page permalink conflict (same slug)

The easiest would be to just disable the archive page for this CPT:

register_post_type( 'visningshus',
    array(
        [...]
        'has_archive' => false,
        [...]
    )
);

Don't forget to refresh you permalinks afterwards at "Settings > Permalinks"


I'm not high enough reputation to comment on @Carl's post, but his is the correct answer (at least to this situation of keeping the page slug and custom post type slug the same and not using an archive.php file)

But his answer would give errors with pagination. To avoid pagination errors:

add_rewrite_rule('^visningshus/page/([0-9]+)','index.php?pagename=visningshus&paged=$matches[1]', 'top');

This looks at the pagination, grabs the page number, and then on the flip side forces it to use the page with the matching slug, and then inserts the page variable.

This in addition to 'has_archive' => false, posted above should achieve the desired functionality.

As others stated, be sure to flush the rewrite rules by visiting Settings > Permalinks after making this change.

Hope this helps someone, because I know this stumped me for a really long time.


Tjena Henrik!

I think all you need to do is to add a rewrite rule to the Wordpress Rewrite rules that have already been created. Add this code snippet to your functions.php of your theme or include it as a separate pugin...

add_action('init', function () {
     add_rewrite_rule('visningshus/?$','index.php?pagename=visningshus', 'top');
     flush_rewrite_rules();
}, 1000);