I'm workin on new webiste and I got stuck :(

Basically I've created a custom post type for products with custom fields etc.

Each product has a main category and subcategory - selected from

<option value="category1">category 1</option>
<option value="category2">category 2</option>

<option value="subcategory1">subcategory 1</option>
<option value="subcategory1">subcategory 2</option>

inside editor.

The subcategory is based on category.

What I'm trying to achieve is to get permalinks based on both: category and subcategory fe.


Any ideas how to achieve this?

  • Show us some code. Have you tried anything? – SLH Nov 1 '14 at 20:13
  • I read about taxonomies, but that doesn't seem to resolve my problem. With one-level slug taxonomies would be great, but I've two... – knysha Nov 1 '14 at 22:12
  • You don't have to write "The subcategory is based on category." What URL structure you are getting when you view product? – Gangesh Nov 2 '14 at 9:26
  • @Gangesh - domain.com/products/name-of-the-product EDIT: /products/ - name of the custom post type /name-of-the-product - title of page created under custom post type – knysha Nov 2 '14 at 12:28
  • Its not exact what you need but very close. wordpress.org/support/topic/… – Gangesh Nov 2 '14 at 19:15

Look at the filter for rewrite_rules_array and add a re-write rule to re-write /products/category-name/sub-category-name/product-name to index.php?pagename=product-name. You might need to grab the category-name and sub-category-name too if your product-names are not unique.

Then add a query var for product-name using the query_vars filter.

Once you've done that in your new post type template you can just access the product-name query var (and the others) using get_query_var. And display the correct record.

Without coding the answer for you that's and outline of how I've done exactly what you're trying to do.

  • Thanks for answer! I tried to rewrite permalink, is it correct? pastebin.com/pAXZw2mH – knysha Nov 2 '14 at 11:24
  • You don't know what the query vars are when the re-write rules are added. You only know when the page (or post) is generated. The rewrite rule should be something like this: array('produkty/([^/]+)/([^/]+)/([^/]+)/?$' => 'index.php?pagename=$matches[3]&product_category=$matches[1]&product_sub_category=$matches[1] – Mark Nemtsas Nov 2 '14 at 20:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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