Input data:
- There is a Custom Post Type, with
Companies
posts that relates
toCompany
category; - There is a Custom Post Type, with
Products
posts that tied toItems -> Item-type
category; - There is a Custom Post Type, with
Articles
posts that tied to
Articles
category;
Summarizing, structure looks like this:
+ Companies (cat)
|- Company-1 (post)
`- Company-2
+ Items (cat)
`- Item-type-1 (cat)
| |- Product-1-1 (post)
| `- Product-1-2
`- Item-type-2 (cat)
|- Product-2-1 (post)
`- Product-2-2
+ Articles (cat)
|- Article-1 (post)
`- Article-2
The Problem:
I’ll be appreciate for a way to implement the structure that can be accessed like this:
http://example.com/company-1/items/item-type-1/product-1
http://examplecom/company-2/items/item-type-1/product-1 //(note that both company 1 and 2 can produce a similar product)
http://example.com/company-1/articles/article-1-1
http://example.com/company-2/articles/article-2-1
Both Products and Articles posts have got a custom fields that stores post-IDs of companies they’re tied to.
UPDATE:
I was thinking about developing it with custom taxonomies:
http://example.com/post-type-company/taxonomy-item/term-item-type/product
Can you advice more suitable way?
In wordpress, the slug (post name) is unique per post type.
So, an url like
/company-1/items/item-type-1/product-1
and one like/company-2/items/item-type-1/product-1
whereproduct-1
is the post name, cannot address to 2 different products.If you create 2 products with same title WordPress on saving will set an unique slug.
Same thing is for articles.
For this reason in a url like:
can simply be rewritten in
and a url like:
can simply be rewritten in
So, as you can see, no matter for category.
You need only 2 rewrite rules:
After that you have to flushing rewrite rules going Settings->Permalink section in your backend and saving changes.
Now the urls work as you expect, if you manually write them on browser, but the problem is generate the right url using
the_permalink
function. You have to use the ‘post_link’ filter and generate the right url:Here I assume you are using only standard categories, where the category ‘Companies’ is the parent category for companies and ‘Items’ is the parent category for items.
Also assumed cpt are named ‘products’ and ‘articles’ (note the plural).
Ajust the functions if something is different in your configuration.