I need to rewrite urls of my custom post type from:
/post_type_slug/post_type_name
to
/post_type_taxonomy_terms/post_type_name
Attached code works fine: url are correctly rewrited BUT I have a 404.

Note:
elementi = post type name
company_cat = custom taxonomy

function item_permalink_structure($post_link, $post, $leavename, $sample)
{
    if ( 'elementi' == get_post_type( $post ) ) {
        if ( false !== strpos( $post_link, 'elementi/%company_cat%' ) ) {
            $item_type_term = get_the_terms( $post->ID, 'company_cat' );

            $item_type_term_parent_def = array();

            foreach ($item_type_term as $item_type_term_parent_obj) {
                //var_dump($item_type_term_parent_obj);
                if ($item_type_term_parent_obj->parent == 0){
                    array_push($item_type_term_parent_def, $item_type_term_parent_obj->slug);
                }
            }
            $item_type_term = implode("+", $item_type_term_parent_def);
            $post_link = str_replace( 'elementi/%company_cat%', $item_type_term, $post_link );
        }
        return $post_link;
    }
}

add_filter('post_type_link', 'item_permalink_structure', 10, 4);

Read more here: Custom permalink with custom taxonomies for custom post type


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: