0
votes

I have 2 custom taxonomies that share same slug and have a custom post, named Courses

1:

'rewrite' => [ 'slug' => 'courses/%tag%', 'with-front' => false ],
'labels'  => [ 'name' => 'Tag' ] 

2:

'rewrite' => [ 'slug' => 'courses/%cat%', 'with-front' => 'false' ],
'labels'  => [ 'name' => 'Category' ]

I rewrote the URL for the custom post type with Category taxonomy so that I can have this URL

courses/math/my-post 

It works, even when I go to courses/math I see all my math posts.

courses/math

But I have also tags, which I want them to be at the same level as the category but I get 404, for example:

courses/logic

How can I achieve so that this 2 taxonomies can live together on the same level ?

This is my rewrite rule for the custom post type.

function courses_post_link( $post_link, $id = 0 ){
    $post = get_post($id);  
    if ( is_object( $post ) ){
        $terms = wp_get_object_terms( $post->ID, 'course_category' );
        if( $terms ){
            return str_replace( '%cat%' , $terms[0]->slug , $post_link );
        }
     }
     return $post_link;  
 }
 add_filter( 'post_type_link', 'courses_post_link', 1, 3 );


 function archive_rewrite_rules() {
     add_rewrite_rule(
        '^courses/(.*)/(.*)/?$',
        'index.php?post_type=courses&name=$matches[2]',
        'top'
     );

}
1

1 Answers

0
votes

Resolved.

I used a plugin called Permalink Customizer.