Custom Post type and Taxonomy using same slug with rewrite Custom Post type and Taxonomy using same slug with rewrite wordpress wordpress

Custom Post type and Taxonomy using same slug with rewrite


After working on this for a week I finally solved the issue. I kept the post types exactly as they were. I replaced the generate_rewrite_rules function with the following.

add_filter('request', 'setTermRequest', 1, 1 );function setTermRequest($query){    $tax_name = 'services';    if( $query['attachment'] ) :        $include_children = true;        $name = $query['attachment'];    else:        $include_children = false;        $name = $query['name'];    endif;    $term = get_term_by('slug', $name, $tax_name); // get the current term to make sure it exists    if (isset($name) && $term && !is_wp_error($term)): // check it here        if( $include_children ) {            unset($query['attachment']);            $parent = $term->parent;            while( $parent ) {                $parent_term = get_term( $parent, $tax_name);                $name = $parent_term->slug . '/' . $name;                $parent = $parent_term->parent;            }        } else { unset($query['name']); }        switch( $tax_name ):            case 'category':{                $query['category_name'] = $name; // for categories                break;            }            case 'post_tag':{                $query['tag'] = $name; // for post tags                break;            }            default:{                $query[$tax_name] = $name; // for another taxonomies                break;            }        endswitch;    endif;    return $query;}add_filter( 'term_link', 'writeTermPerm', 10, 3 );function writeTermPerm( $url, $term, $taxonomy ){    $taxonomy_name = 'services';    $taxonomy_slug = 'services';    if ( strpos($url, $taxonomy_slug) === FALSE || $taxonomy != $taxonomy_name ) return $url;    $url = str_replace('/'.$taxonomy_slug, '/portfolio', $url);    return $url;}

After that fixed the url structure I added the following code to my taxonomy-services.php document in order to control the entire system in one file.

locate_template( 'archive-portfolio.php', true );