1

I have a custom post type with a custom taxonomy.

The custom post type is news.

The custom taxonomy is new_category.

What I want is the possibility two operate link that:

http://example.com/news/ -> show news archive
http://example.com/news/movies/ -> show category movie archive
http://example.com/batman-returns/ -> show post content called "Batman Returns"

I tried this, but doesn't work:

add_rewrite_rule('^news/([^/]*)/?','index.php?post_type=news&news=$matches[1]','top');
add_rewrite_rule('^news/([^/]*)/?','index.php?post_type=news&news_category=$matches[1]','top');

Suggestions?

1 Answer 1

1

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/', '/', $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%/.

5
  • Thanks @Dan but i was wrong about the final part that I wanted. Sorry. Your solution was perfect and fit my question, but I need this: http://example.com/news/ -> show news archive http://example.com/news/movies/ -> show category movie archive http://example.com/news/batman-returns/ -> show post content called "Batman Returns" Jun 9, 2016 at 13:31
  • @MarcosRezende There're post post type and news post type. What is archive/post content of what post type? I can't get what you meant.
    – wpclevel
    Jun 9, 2016 at 13:40
  • I have post post_type and I have news post_type. The thing is that I dont want example.com/batman-returns but example.com/news/batman-returns When I typed my question before I typed wrong. I sad example.com/batman-returns but I want example.com/news/batman-returns Do you understand? Sorry about my mistake. Jun 9, 2016 at 14:56
  • @MarcosRezende We can't do that because one rewrite rule can't match both category archive (news/movies/) and post content (/news/batman-returns/). WordPress can't handle it.
    – wpclevel
    Jun 9, 2016 at 15:02
  • Thanks for your support @Dan. I made /news/news_category/ and /news/news_category/batman-returns/ Jun 9, 2016 at 16:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.