Skip Navigation

[Resolved] Rewrite Post Type Slug but with php

This support ticket is created 7 years ago. 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.

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)

Tagged: 

This topic contains 3 replies, has 2 voices.

Last updated by Luo Yang 7 years ago.

Assisted by: Luo Yang.

Author
Posts
#587361

Hello,

I have created a post type with the Types plugin and I need to rewrite it's slug. I need to rewrite the slug if specific conditions exist, so I can not do it through the dashboard. How I could rewrite the slug of a post type that was created with types but with php?

Thank you!

#587369

Dear Ioannis,

I suggest you use WordPress built-in filter hooks to change the CPT settings:

For custom post types, please try with filter hook "register_post_type_args"
https://developer.wordpress.org/reference/hooks/register_post_type_args/

See the example codes here:
https://developer.wordpress.org/reference/hooks/register_post_type_args/#user-contributed-notes

#587554

Hello Luo,

That works perfectly for the base of the post type but I am trying to change the post type's title slug in url. I would like to replace the title with the value of a custom field if specific conditions exist. Is that possible?

#587868

There isn't such a built-in feature within Toolset plugins, to change the post slug, please try with wordpress function wp_update_post()
https://developer.wordpress.org/reference/functions/wp_update_post/

For example, when user save/edit a post, use wordpress action hook save_post to trigger a custom PHP function,
https://developer.wordpress.org/reference/hooks/save_post/

In this PHP function, get the value of custom field,
https://developer.wordpress.org/reference/functions/get_post_meta/
If the condition is satisfied, then update the post_name as what you want.