I’ve got a question. Maybe it’s something existing and I don’t know the name. Or maybe it’s something custom.

I’ve got a post type for news. The news posts have categories. These are some examples of separate news items

  • News item 1.

    • Category “purple”
  • News item 2.

    • Category “purple”
    • Category “red”
  • News item 3.

    • Category “red”

I have 3 pages where I show and filter the news:

  • www.domain.com/news (This shows all the news)
  • www.domain.com/purple/news (This shows all the purple news)
  • www.domain.com/red/news (This shows all the purple news)

Showing a filtered post grid on each news page is not the problem. The problem is displaying them. By default the permalinks of each news post is:

  • www.domain.com/news/news-item-1
  • www.domain.com/news/news-item-2
  • www.domain.com/news/news-item-3

And this is fine for the news on www.domain.com/news/

But now I want to display the category news items like following: From www.domain.com/purple/news I want to click on the news item and open it like:

  • www.domain.com/purple/news/news-item-1
  • www.domain.com/purple/news/news-item-2

And from www.domain.com/red/news I want to click on the news item and open it like:

  • www.domain.com/red/news/news-item-2
  • www.domain.com/red/news/news-item-3

The Question

How do I get the news on the category news pages to open and show the items on the preferred url? (this should also show up in the sitemap.xml)

  • And why do you want to reverse order of slugs in the URL? Why not use example.com/news/purple/ and example.com/news/purple/first-news-slug/ instead? (It's much more natural and intuitive...) – Krzysiek Dróżdż Nov 29 at 10:58
  • @KrzysiekDróżdż, because example.com/purple is sort of a landing page for this category. With a new page and some other pages under it. – Tim Nov 29 at 12:22
  • Posts should have a single canonical URL, otherwise search engines will penalize you for duplicate content. – Milo Nov 29 at 14:31
  • @Milo, thanks for bringing that to my attention. I'll use "rel="canonical" link element" for this duplicates to fix it. Source: support.google.com/webmasters/answer/66359?hl=en – Tim Nov 29 at 14:47

you can use Custom Permalinks plugin. you can create and edit each post and category link very essay. https://wordpress.org/plugins/custom-permalinks/ Try this once

up vote 0 down vote accepted

I've found a solution. This is the abstracted version:

add_rewrite_rule(
     '^red\/news([^/]*)/?',
     'index.php?event=$matches[1]',
     'top'
);

//Ensure the $wp_rewrite global is loaded
global $wp_rewrite;
//Call flush_rules() as a method of the $wp_rewrite object
$wp_rewrite->flush_rules( false );

In the actual function I've made an array with the pages I want to redirect. (Source and destination) and run foreach and flush the rules after.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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