• Resolved janc76

    (@janciriack)


    I have a custom post type Presse which archive is available at /presse. The archive is only called with taxonomies, e.g. /presse/post_tag/abc, /presse/year/2019, never as the full archive /presse.
    I also have a page Presse having the URL /presse as well. It acts as an overview page for the terms of that CPT.
    I faced the problem that the CPT archive had overwritten the page address as both have the same URL. Renaming URLs wasn’t an option for me.

    I found out that there was a rewrite rule
    presse/?$ -> index.php?post_type=presse
    causing the conflict so I bluntly took it out with

    add_filter( 'rewrite_rules_array', 'remove_presse_rewrite_rule' );
    function remove_presse_rewrite_rule( $rules ) {
        foreach ( $rules as $rule => $rewrite ) {
            if ( preg_match( '/presse\/\?\$/', $rule ) ) {
                unset( $rules[$rule] );
            }
        }
        
        return $rules;
    }

    That solved my problem, however I would like know if this is a proper way of dealing with this kind of issue or if there are cleaner ways?

    The page I need help with: [log in to see the link]

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    The cleaner way would have been to avoid creating the conflict ?? Ideally we would prevent the unneeded rule from being created when the post type is registered, but I don’t see a way to prevent that one rewrite rule without also preventing all related rewrite rules. AFAIC what you have done is fine. Mainly because I don’t see a better alternative short of changing either the post or page slug.

    Thread Starter janc76

    (@janciriack)

    Thank you for your opinion. Good to know that my approach is not entirely dirty and there is obviously no straight way to solve this.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Page/CPT URL conflict’ is closed to new replies.