Remove cpt slug from url and use custom taxonomy instead breaks all other cpt

Question

Im trying to do so, it works but also affects all other cpt (404 for all cpt but not resources one)

this is what I have

In the cpt definition

...

        'rewrite' => array(
                      'slug' => 'resource/%resource_type%',
                       'with_front' => false
                     )
...

the post link filter

// Rewrite urls of resources and put category on url
function wpa_course_post_link( $post_link, $id = 0 ){
    $post = get_post($id);  
    if ( is_object( $post ) ){
        $terms = get_the_terms( $post->ID, 'resource_type' );
        
        if(get_post_type($post->ID) == 'resource' ) {
            if( $terms ){
                return str_replace( 'resource/%resource_type%', $terms[0]->slug, $post_link );
            }else{
                return home_url( "/resource/".$post->post_name );
            }
        }else{
            return $post_link; 
        }
    }
}
add_filter( 'post_type_link', 'wpa_course_post_link', 1, 3 );

and the problem here, the rewrite rule, if I comment this action all works but the resources does not(404), and if I uncomment this resources works (with the custom url) but all the other cpt does not (404)… so i dont know

function resource_rewrite_rules() {
    add_rewrite_rule(
        '^(.*)/(.*)/?$',
        'index.php?post_type=resource&resource_type=$matches[1]&name=$matches[2]',
        'top'
    );
        
}
add_action( 'init', 'resource_rewrite_rules' );

any help?

0
user4239 4 months 2022-04-12T18:07:01-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse