Skip Navigation

[Gelöst] Need to control order in which rewrites for CPTs are processed (404 problem)

This support ticket is created vor 7 Jahre, 7 Monate. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 4 Antworten, has 2 Stimmen.

Last updated by ashleighP vor 7 Jahre, 7 Monate.

Assisted by: Luo Yang.

Author
Artikel
#435928

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?

#436349

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.

#436606
Screen Shot 2016-09-15 at 3.07.46 PM.png
Screen Shot 2016-09-15 at 3.05.35 PM.png

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.

#436716

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.

#438735

Thank you!

The code worked.

Maybe for a future iteration of types there could be a bug fix.

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.