I have created hierarchy based custom permalinks with the help of this link, but now it is causing a 404 error on another custom taxonomy used in the site. I want custom permalinks on this other taxonomy also, but if I don’t use any rewrite arguments and leave it on its default permalink, it is still not working.
My code for the second taxonomy:
register_post_type(
'gallery',
array(
'public' => true,
'menu_position' => 15,
'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ),
'taxonomies' => array( '' ),
'menu_icon' => get_template_directory_uri() . '/img/icon_gallery.png',
'register_meta_box_cb' => 'add_gallery_metaboxes'
)
);
register_taxonomy(
'gallery_category',
'gallery',
array(
'hierarchical' => true,
'labels' => $labels
)
);
And here is my first custom taxonomy code:
register_post_type(
'article',
array(
'public' => true,
'hierarchical' => true,
'menu_position' => 15,
'supports' => array( 'title', 'editor', 'comments', 'thumbnail' ),
'taxonomies' => array( '' ),
'menu_icon' => get_template_directory_uri() . '/img/icon_article.png',
'query_var' => true,
'rewrite' => array(
'slug' => '%article_category%/articles',
'with_front' => true
),
'has_archive' => '%article_category%',
'register_meta_box_cb' => 'add_article_metaboxes'
)
);
register_taxonomy(
'article_category',
'article',
array(
'hierarchical' => true,
'labels' => $labels,
'query_var' => true,
'rewrite' => array( 'slug' => '', 'hierarchical' => true ),
)
);
Function for custom permalink:
function filter_post_type_link( $link, $post ) {
if ( $post->post_type != 'article' )
return $link;
if ( $cats = get_the_terms( $post->ID, 'article_category' ) ) {
$link = str_replace( '%article_category%', get_taxonomy_parents( array_pop( $cats )->term_id,
'article_category', false, '/', true ), $link ); // See custom function defined below
}
return $link;
}
add_filter( 'post_type_link', 'filter_post_type_link', 10, 2 );
After many efforts I have solved it, but it’s not completely what I want, as I had to compromise on one thing and passed some separators in the URL. Example:
In order to avoid 404 errors, in case of pagination and another custom taxonomy, I created some rewrite rules. Here is my code:
First post type and custom taxonomy registration code:
In the first custom taxonomy, I used ‘articles’ and ‘topic’ as separators; you can use any according to your scenario.
Second post type and custom taxonomy registration code:
In the second custom taxonomy, I used ‘galleries’ and ‘images’ as separators; you can use any according to your scenario.
The function for rewrite rules:
The functions to change the custom post type URL according to custom taxonomy hierarchy: