This is an extension of this thread: https://toolset.com/forums/topic/permalinks-for-new-cpt-not-working/
I have a parent CPT called Events. Events has children, including Proofs and Timelines.
The URL for the CPT Events is set to rewrite to charmcat.net/account/event/%title% which works perfectly.
The URL for the CPT Proofs is set to rewrite to charmcat.net/account/event/proof/%title% which works perfectly.
These two CPTs were created some time ago. I am adding Timelines as a new CPT.
I want to set the URL for the CPT Timelines to charmcat.net/account/event/timeline/%title% which results in a 404.
I used the plugin Debug This to examine the rewrite rules and found out the problem. Because the rule for account/event comes before the rule for account/event/timeline, WordPress is resolving any Timeline post permalink with the Event post rewrite. The rewrite rule for account/event/proof comes before account/event, which is why that one is working fine.
Googling shows that it's possible to change a rewrite rule to have a top or bottom priority but I have no idea how to do this, and of course there's no GUI for it through types. How can I fix this?
Dear ashleigh,
You can change the rewrite rule of custom post type, for example, edit the post type "Timelines", there is an option "Rewrite", you can setup the custom rewrite rule there:
https://toolset.com/documentation/user-guides/create-a-custom-post-type/#even-more-advanced-custom-post-types-settings
Rewrite – rewrite post types permalinks. You can use the normal WordPress URL logic (which you will have set up in Settings > Permalinks) or you can use a custom URL format. The default for this is “true” and it will use the slug you defined earlier.
Yes, that's what I did.
My problem is that I want the new rewrite rule for the Timeline CPT to take precedence over the rewrite rule for Event.
I attached a shot showing the rewrite rules and how it's matching the rewrite rule. I want to tell WordPress that even though the CPT Event was created first I want its rewrite rules to be matched last.
Thanks for the details, I can duplicate same problem, I assume we are talking about the rewrite rules as below:
Parent post type "Events", rewrite rule "account/event"
Child post type "Proofs", rewrite rule "account/event/proof"
Child post type "Timelines", rewrite rule "account/event/timeline"
And if you need to change a rewrite rule to have a top or bottom priority, this needs custom codes, here are some documents for your refrence:
https://codex.wordpress.org/Rewrite_API/add_rewrite_rule
add_rewrite_rule() allows you to specify additional rewrite rules for WordPress. It is most commonly used in conjunction with add_rewrite_tag() (which allows WordPress to recognize custom post/get variables).
For example:
1) You can try to add below codes into your theme/functions.php:
add_filter('rewrite_rules_array', 'cpt_rewrites_func');
function cpt_rewrites_func($rules){
$res1 = array();
$res2 = array();
foreach ($rules as $rule => $rewrite) {
if (strpos($rule, 'account/event/proof') !== false || strpos($rule, 'account/event/timeline') !== false) {
$res1[$rule] = $rewrite;
}
else{
$res2[$rule] = $rewrite;
}
}
return array_merge($res1, $res2);
}
2) Dashboard-> Settings-> Permalink
click button "Save changes", and test again.
Thank you!
The code worked.
Maybe for a future iteration of types there could be a bug fix.