I have rewritten my taxonomy url to the name of my posts type 'filter'.
Because I have done this, my pagination does not work anymore on the url:
/
So if I visit
I get a 404
I think this is because Wordpress sees filter now as the post type and the taxonomy.
If I change 'slug' => 'filter'
, to for example 'slug' => 'test'
it works fine. Does anyone know how I can say to Wordpress that 'filter' is in this case the taxonomy.
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => array( 'slug' => 'filter', 'with_front' => false ),
);
register_taxonomy( 'service', 'filter', $args );
Any suggestion to why this is not working?
I have rewritten my taxonomy url to the name of my posts type 'filter'.
Because I have done this, my pagination does not work anymore on the url:
http://www.website/filter/term-name/
So if I visit
http://www.website/filter/term-name/page/2
I get a 404
I think this is because Wordpress sees filter now as the post type and the taxonomy.
If I change 'slug' => 'filter'
, to for example 'slug' => 'test'
it works fine. Does anyone know how I can say to Wordpress that 'filter' is in this case the taxonomy.
$args = array(
'labels' => $labels,
'hierarchical' => true,
'public' => true,
'show_ui' => true,
'show_admin_column' => true,
'show_in_nav_menus' => true,
'show_tagcloud' => true,
'rewrite' => array( 'slug' => 'filter', 'with_front' => false ),
);
register_taxonomy( 'service', 'filter', $args );
Any suggestion to why this is not working?
Share Improve this question edited Mar 24, 2014 at 9:44 Pieter Goosen 55.5k23 gold badges117 silver badges211 bronze badges asked Mar 24, 2014 at 8:23 RobbertRobbert 1,2756 gold badges23 silver badges47 bronze badges 2- Filter is your custom post type or what?, you are registering service taxonomy for filter object and than you are rewriting the same taxonomy to filter. – Kumar Commented Mar 24, 2014 at 10:31
- @sven Yes that is true. – Robbert Commented Mar 24, 2014 at 12:31
3 Answers
Reset to default 1While 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%/.
I know this question is quite old but there are many people still looking for this answer as I was earlier today. The code below is assuming that you have both your Custom Post Type and Taxonomy sharing the same slug. In this example the post type is portfolio and the taxonomy is services
function generate_taxonomy_rewrite_rules( $wp_rewrite ) {
$rules = array();
$post_types = get_post_types( array( 'name' => 'portfolio', 'public' => true, '_builtin' => false ), 'objects' );
$taxonomies = get_taxonomies( array( 'name' => 'services', 'public' => true, '_builtin' => false ), 'objects' );
foreach ( $post_types as $post_type ) {
$post_type_name = $post_type->name; // 'developer'
$post_type_slug = $post_type->rewrite['slug']; // 'developers'
foreach ( $taxonomies as $taxonomy ) {
if ( $taxonomy->object_type[0] == $post_type_name ) {
$terms = get_categories( array( 'type' => $post_type_name, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0 ) );
foreach ( $terms as $term ) {
$rules[$post_type_slug . '/' . $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug;
$rules[$post_type_slug . '/' . $term->slug . '/page/?([0-9]{1,})/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug . '&paged=' . $wp_rewrite->preg_index( 1 );
}
}
}
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'generate_taxonomy_rewrite_rules');
I have the same problem but finally i resolved with below code added into functions.php of theme folder.
function generate_taxonomy_rewrite_rules( $wp_rewrite ) {
$rules = array();
$post_types = get_post_types( array('public' => true, '_builtin' => false ), 'objects' );
$taxonomies = get_taxonomies( array('public' => true, '_builtin' => false ), 'objects' );
foreach ( $post_types as $post_type ) {
$post_type_name = $post_type->name;
$post_type_slug = $post_type->rewrite['slug'];
foreach ( $taxonomies as $taxonomy ) {//echo 'DDDDD'; print_r($taxonomy);
if ( $taxonomy->object_type[0] == $post_type_name ) {
$terms = get_categories( array( 'type' => $post_type_name, 'taxonomy' => $taxonomy->name, 'hide_empty' => 0 ) );
//print_r($terms);
foreach ( $terms as $term ) {
$rules[ $taxonomy->name.'/'. $term->slug . '/?$'] = 'index.php?' . $term->taxonomy . '=' . $term->slug;
$rules[ $taxonomy->name.'/'. $term->slug . '/page/?([0-9]{1,})/?$'] = 'index.php?'.'type='.$post_type_name."&" . $term->taxonomy . '=' . $term->slug . '&paged=' . $wp_rewrite->preg_index( 1 );
}
}
}
}
$wp_rewrite->rules = $rules + $wp_rewrite->rules;
}
add_action('generate_rewrite_rules', 'generate_taxonomy_rewrite_rules');
REMEMBER MY $taxonomy->name and post type is similar. hope it will work.