• I’m using a CPT (wg_seo_chat) together with a Custom taxonomy (wg_chat_category). Additional rewrite rules are in place to inject the Taxonomy into the URL:

      CPT archive: domain/hilfe-chat
      Taxonomy archive: domain/hilfe-chat/allgemein
      Item page: domain/hilfe-chat/general/item

    I’d like to keep the same slug /hilfe-chat/ for both CPT and Taxonomy archives, which seems to generate a conflict (404 for Taxonomy archives).

    CPT

    $rewrite = array(
            'slug'                  => 'hilfe-chat',
            'has_archive'           => 'hilfe-chat',

    Custom taxonomy

    $args = array(
                'rewrite'           => array('slug' => 'hilfe-chat')
            );

    Rewrite rules for custom taxonomy

    /** Rewrite rule to handle third level slug */
    
            function chat_cpt_generating_rule($wp_rewrite) {
                $rules = array();
                $terms = get_terms( array(
                    'taxonomy' => 'wg_chat_category',
                    'hide_empty' => false,
                ) );
               
                $post_type = 'wg_seo_chat';
            
                foreach ($terms as $term) {    
                            
                    $rules['hilfe-chat/' . $term->slug . '/([^/]*)$'] = 'index.php?post_type=' . $post_type. '&wg_seo_chat=$matches[1]&name=$matches[1]';
                                    
                }
            
                // merge with global rules
                $wp_rewrite->rules = $rules + $wp_rewrite->rules;
            }
            add_filter('generate_rewrite_rules', 'chat_cpt_generating_rule');
    
            /** Inject the chat category into the CPT Link */
    
            function change_link( $permalink, $post ) {
        
                if( $post->post_type == 'wg_seo_chat' ) {
                    $chat_terms = get_the_terms( $post, 'wg_chat_category' );
                    $term_slug = '';
                    if( ! empty( $chat_terms ) ) {
                        foreach ( $chat_terms as $term ) {
            
                            // The featured chat will have another category which is the main one
                            if( $term->slug == 'featured' ) {
                                continue;
                            }
            
                            $term_slug = $term->slug;
                            break;
                        }
                    }
                    $permalink = get_home_url() ."/hilfe-chat/" . $term_slug . '/' . $post->post_name;
                }
                return $permalink;
            
            }
            add_filter('post_type_link',"change_link",10,2);

    Any thoughts on how to resolve this conflict are much appreciated.

    Thanks, Jan

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

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    You cannot normally have the same slug refer to two different object types. If you really must do so, you’d need to intercept the request parsing process (“request” filter perhaps?) and make a trial query to see if the request matches object type A. If so, alter query vars to get type A, otherwise alter to get type B.

    Even then, you cannot have individual objects of different types have the same slug since type A will always take precedence, so one can never get to type B with the same name.

    Because this involves making an extra query, it’s not as performative as normal requests. These drawbacks are why the same slug should not refer to two different object types.

Viewing 1 replies (of 1 total)
  • The topic ‘Same slug for Custom Taxonomy archive and CPT archive – Rewrite rule not working’ is closed to new replies.