How to use custom taxonomy in the permalinks of default post type ‘post’?
-
I am using the default post type ‘post’ of the WP as the ‘articles’ for one of my projects. I have a case where I’d like to use a custom taxonomy called ‘article_types’, defined explicitly for the post type ‘post’, to be used in permalinks like this.
/%articles_types%/%postname%.htmlI am also looking for a way to enforce it programmatically so that it is not accidentally changed using the permalink settings page.
Things I’ve tried so far.
1. Just as a proof of concept, I first decided to set the permalink structure using the UI. First thing I notice, I don’t see the ‘%article_types%’ placeholder to select from (I think it’s called perma_struct in WP Lingo). Still I tried manually typing it in the custom permalink field and hit save. The result is that I do see the article URLs changed to this format but the %article_types% is not getting replaced with the assigned “article types” to the article. Like so,/%article_types%/name-of-the-post.htmlinstead of the/news/name-of-the-post.html.
2. I tried this same thing with code, using the $wp_rewrite’sset_permalink_structure()function. Since it’s the same thing as #1, I didn’t see anything different. The online material shows that I’ll have to use the filterpre_post_linkand replace the%article_types%with whatever the value that is assigned to the post.My doubts with above approaches is that I suspected the placeholder will be replaced automatically by the WP but it isn’t. Am I doing something wrong? Or it is perfectly fine but it’s just that
article_typesis defined on a particular post type only, I would have to handle the case manually.
And that brings another doubt in the picture, when I set the permalink_structure using the function, am I inadvertently asking WP to add that placeholder for all the post types? Are the pages and other default post types going to get affected by it? Or the permalink settings page is only specifically for the ‘post’ post type? I am asking this because the documentation of theset_permalink_structuremethod states that itSets the main permalink structure for the site.3. This approach is different from the other two where I tried hacking around with the post type arguments while the registration of the default post type. So basically, I did this,
add_filter( ‘register_post_type_args’, function ( $args, $post_type ) {
global $wp_rewrite;
if ( $post_type === ‘post’ && ! empty( $wp_rewrite ) ) {
$args[‘rewrite’] = array(
‘slug’ => ‘/%article_types%/%postname%.html’,
‘with_front’ => false,
);
}return $args;
}, 10, 2 );
But still no luck. The permalinks of the ‘post’ remain as is. When I say as is, I mean to say that they follow what has been set in the set_permalink_structure OR the permalink settings page.How should I approach this problem? I would try to avoid creating a custom post type named articles at this point. Any other ideas are highly appreciated.
The topic ‘How to use custom taxonomy in the permalinks of default post type ‘post’?’ is closed to new replies.