1. Change new_category
taxonomy’s rewrite rule while registering:
'rewrite' => ['hierarchical' => false, 'with_front' => false, 'slug' => 'news']
Make sure rewrite rule of news
post type is default or:
'rewrite' => ['slug' => false, 'with_front' => false]
2. Add rewrite rules for new_category
:
add_action('init', function()
{
add_rewrite_rule('^news/([^/]+)/?$', 'index.php?new_category=$matches[1]&post_type=news', 'top');
});
3. Filter news
post type links:
add_filter('post_type_link', function($post_link, $post, $leave_name = false, $sample = false)
{
if ( 'news' === $post->post_type)
{
$post_link = str_replace('/news/', "https://wordpress.stackexchange.com/", $post_link);
}
return $post_link;
}, 10, 4);
4. Add news
post type to query vars:
By remove news
from permalink, WordPress will query posts from post
post type and you will get 404 not found now. So we need to add our news
post type to query vars.
add_action('pre_get_posts', function($query)
{
if ( $query->is_main_query() && ( 2 === count($query->query) ) && isset($query->query['name']) )
{
$query->set('post_type', ['post', 'news', 'page']);
}
});
That’s all. Flush your permalink structure and make sure it is set to /%postname%/
.