While looking for a solution I got this post: Taxonomies with same slug as CPT
So basically you need to add custom rewrite rules, I haven't tested the solution but I guess it will work out for you.
/* Register CPT
*/
function wpse_138987_post_type_filter() {
register_post_type('filter', array(
'labels' => array(
'name' => 'Filter',
'all_items' => 'All Posts'
),
'public' => true
));
}
add_action('init', 'wpse_138987_post_type_filter');
// register taxonomy
function wpse_138987_taxonomy_service() {
register_taxonomy('service', array('filter'), array(
'labels' => array(
'name' => 'Service'
),
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => array('slug' => 'filter')
));
}
add_action('init', 'wpse_138987_taxonomy_service');
/*
* Replace Taxonomy slug with Post Type slug in url
*/
function taxonomy_slug_rewrite($wp_rewrite) {
$rules = array();
// get all custom taxonomies
$taxonomies = get_taxonomies(array('_builtin' => false), 'objects');
// get all custom post types
$post_types = get_post_types(array('public' => true, '_builtin' => false), 'objects');
foreach ($post_types as $post_type) {
foreach ($taxonomies as $taxonomy) {
// go through all post types which this taxonomy is assigned to
foreach ($taxonomy->object_type as $object_type) {
// check if taxonomy is registered for this custom type
if ($object_type == $post_type->rewrite['slug']) {
// get category objects
$terms = get_categories(array('type' => $object_type, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0));
// make rules
foreach ($terms as $term) {
$rules[$object_type . '/' . $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug;
}
}
}
}
}
// merge with global rules
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_filter('generate_rewrite_rules', 'taxonomy_slug_rewrite');
The above solution works only if you are using Permalinks with Post name.
Make sure you save permalink after adding the code, to flush rewrite rules.
Also you need to add files for Custom Post Type Archive and Taxonomy:
archive-filter.php
single-filter.php
taxonomy-service.php
Go to settings/permalinks and save changes using structure /%postname%/.