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!
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
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?
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.