The situation

I have a custom post type, lets call it 'CP', which has two parent categories named 'companies' and 'category'. Posts of type CP can only have one company and multiple categories. So, the selection can be like this:

  • Companies
    • Company A
    • Company B
    • Company C
    • Company D
    • Etc...
  • Categories
    • Food
    • Health
    • Tech
    • Car
    • Living

When a CP-post is created, the url as it is now will be something like this: http://website-url.com/CP/companies/company-c/postname or http://website-url.com/CP/categories/health/postname depending on which category has the highest ID.

The goal

Having an url that looks like http://website-url.com/company-c/postname. So the custom-post CP must be filtered out of the url, as well as the parent category. Also only the child category of the parent category 'companies' has to be used, not the one with the highest category ID.

The problem

All the goals apart can be accomplished, but fixing it all together seems to be impossible. Firstly filtering the custom post type out of the url, enough topics on how to do that, which also work. Like: Remove custom post type slug from URL

Secondly filtering the parent category out of the url, also works independently. See: Remove parent category from permalink? Basically only have the child category?

Lastly selecting which category to use in the url, there's an plugin for that: https://wordpress.org/plugins/hikari-category-permalink/

Spending multiple evenings on how to combine these solutions didn't result in a solid solution. I also am aware of the fact that it's not always the smartest thing to trying to tweak Wordpress permalinks too much, so if anyone has any other suggestion I am willing to accept those.

  • Post your code please, working or not.
    – s_ha_dum
    May 8, 2015 at 13:15
  • @s_ha_dum All the code/plugins used can be found in the links provided. I've tested a lot of different combinations and possibilities which all didn't work as expected. May 8, 2015 at 17:14
  • 1) Third party code is off-topic and 2) whatever code you may have started with will be out of context. 3) Code should be posted into the question and not hosted elsewhere so the question is as self-contained as possible-- this is stack policy.
    – s_ha_dum
    May 8, 2015 at 19:03


Your Answer

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