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:
- Company A
- Company B
- Company C
- Company D
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.
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.
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.