How to make permalink structure tegory%/%postname%/ work for custom post type?

I’m trying to achieve the following permalink structure for a custom post type called “concept” with taxonomy “category

http://domain/categoryname/conceptname

Note: The main problem here is I don’t want the custompost type slug /concept to appear, and now it seems to be conficting with existing rewrite rules

Read More

I did

global $wp_rewrite;
$concept_permalink_structure = '/%category%/%concept%/';
$wp_rewrite->add_rewrite_tag('%concept%/','([^/]+)', 'concept=');
$wp_rewrite->add_permastruct('concept/',$concept_permalink_structure,false );

and

add_filter('post_type_link', 'tt_concept_permalink2', 10,3);   

function tt_concept_permalink2($permalink, $post, $leavename ) {
    
    if($post->post_type != 'concept')
            return $permalink;

    if( !isset($post->ID) ){
        global $post;
    }


    $categories = get_the_terms( $post->ID, 'category');
    
    if( is_array($categories) && count( $categories ) > 0 ) {
        $cat = $categories[key($categories)]->slug;
        $permalink = str_replace('concept/', $cat .'/', $permalink );
    }

    return $permalink;

}

and it is working!

But!

all other permalink( page, other custom post types ) are giving 404 error!

How can I make them work?

Related posts