Writing Custom Rewrite Rules that Incorporate Category for Custom Post Types?
-
16-10-2019 - |
Question
I'm registering a Custom Post Type and in the rewrite array I am trying to do something like this:
'rewrite' => array('slug' => "explore/resources/".$CATEGORY, 'with_front' => false)
I want the rule to 'dynamically' get the post's category name somehow. I also tried using %category%
there but it doesn't work either. I need the post's url to represent the entire path/route to it. Any ideas on how to achieve this?
UPDATE: Here is more clarification:
I have a custom post type of 'resources
' and these post have several different categories like 'forms
', 'mp3
' etc. now this Resources area in the site is under another section (that is a page with custom loop for those custom posts) which is called Explore. What I want to achieve is:
domain.com/Explore/Resources/Resource-Category/Resource-Name
or
domian.com/explore/resources/forms/production-form-1
My current rewrite rule in register_post_type()
is:
'rewrite' => array('slug' => "explore/resources", 'with_front' => false)
.
Thanks!
Solution
You could use my plugin:
http://wordpress.org/extend/plugins/custom-post-permalinks/
That will only work if it's a non-hierarchical post type.
If it's hierarchical, you're going to have to register the permastruct yourself. This is a huge ordeal which I don't have the time to write out at the moment. You can look at the code in my plugin above and try to work it out for yourself. Some pointers:
- In order to get the category into the URL, you're going to have to hook into the
'post_type_link'
- You'll also have to hook into
'parse_request'
to make sure the post type is recognized.