Skip Navigation

[Resolved] Update a custom field with the current slug on save

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
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 5 replies, has 2 voices.

Last updated by Minesh 1 year, 9 months ago.

Assisted by: Minesh.

Author
Posts
#2548045

Tell us what you are trying to do?

I'm trying to use the slug as a way to filter through posts on Toolset. I understand that there is no support for a filter query based on the page's slug but I figured it could work if I automatically update a custom field with the value of the post's slug using the update_post_meta function. This is just a little beyond my skills.

I have a post type called Meetings (slug: meeting) which autogenerates a unique slug on creation. So far, I've created the new custom field in Toolset called Meeting Token (slug: meeting-token). I'm not sure how I could either adjust the function I've included below to do it all at once or create a second function to update the custom field.

function func_update_unique_custom_slug( $data, $postarr ) {

if($data['post_status']=='auto-draft' and $data['post_type']=='meeting')
$data['post_name'] = uniqid();

return $data;
}
add_filter( 'wp_insert_post_data', 'func_update_unique_custom_slug', 99, 2 );

Is there any documentation that you are following?

I used this post to create a unique token for each post automatically. https://toolset.com/forums/topic/random-number-as-permalink/

What is the link to your site?

The site is currently in local development.

#2548541

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Can you please tell me with what value you want to update the custom field "meeting-token"?

You want to update the custom field value when you create new post from backend - correct?

#2549191

The goal is have the value of the 'meeting-token' match the URL slug.

#2549255

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Do you mean that the automatic slug created using the function uniqid() or you can say this uniquid() is assigned to post_name (post slug) you want to copy the same post slug to your custom field (meeting-token) and copy the uniqid() value to custom field (meeting-token) value?

#2549521

Exactly. My goal is have the meeting token match the uniquely generated slug.

#2549969

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

What if you try to add the save post hook:

function func_set_custom_field_value( $post_id, $post ){
    if ( 'meeting' == $post->post_type ) {

        update_post_meta( $post_id, 'wpcf-meeting-token', $post->post_name);       
    }
}
add_action( 'save_post', 'func_set_custom_field_value', 999, 2 );

More info:
- https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/