Why does post type-term archive [ / ] resolve to taxonomy-term archive [ / ]?

Question

sorry to ask this but im currently on a 43-tabbed-6-hr search and studying session to make this work but I think i’ve got some of the things mixed up.

Goal:
Have separate page for each:

[1] domain.com/cpt_arch_1/parent_term1/post_1_in_cpt_1/ [post specific under cpt_1]
[2] domain.com/cpt_arch_2/parent_term1/post_1_in_cpt_2/ [post specific under cpt_2]

[3][x] domain.com/cpt_arch_1/parent_term1 [archive for post under *parent_term1 in *CPT_1]
[4][x] domain.com/cpt_arch_2/parent_term1 [archive for post under *parent_term1 in *CPT_2]

[5] domain.com/cpt_arch_1 [archive for all post in CPT_1]
[6] domain.com/cpt_arch_2 [archive for all post in CPT_2]

[7] domain.com/ct/parent_term1 [to combine post from 2 different CPT_arch]

Custom post type setup background:

CPT_1

'has_archive' => 'cpt_arch_1',
'taxonomies' => array('ct-1'),
$rewrite = array(
    'slug' => 'cpt_arch_1/%ct-1%',
     ...
);

CPT_2

'has_archive' => 'cpt_arch_2',
'taxonomies' => array('ct-1'),
$rewrite = array(
    'slug' => 'cpt_arch_2/%ct-1%',
     ...
);

while my custom taxonomy [ct-1] has:

,"rewrite" => array(
            "slug" => 'ct'
        )

[ct-1] also has the follow terms:

  • parent_term1

it works for everything except 3 & 4 because when i visit either domain.com/cpt_arch_1/parent_term1/ or domain.com/cpt_arch_2/parent_term1/ it just gives me to page for domain.com/ct/parent_term1/ which is the archive page for the term parent_term1 under ct-1 taxonomy. Is there a way I could create an archive page for specific term under a custom post type?

here’s the post_link code I’m currently using:

add_filter('post_type_link', 'update_permalink_structure', 10, 2);
function update_permalink_structure( $post_link, $post )
{
    if ( false !== strpos( $post_link, '%ct-1%' ) ) {
        $taxonomy_terms = get_the_terms( $post->ID, 'ct-1' );
        foreach ( $taxonomy_terms as $term ) { 
            if ( ! $term->parent ) {
                $post_link = str_replace( '%ct-1%', $term->slug, $post_link );
            }
        } 
    }
    return $post_link;
}

Would highly appreciate any input senpais! 🙏

0
ardv 1 month 0 Answers 9 views 0

Leave an answer