Skip Navigation

[Resolved] update_post_meta on a toolset field is not working in save_post

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
- 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, 2 months ago.

Assisted by: Minesh.

Author
Posts
#2637219

I have a CPT created with Toolset that has a slug of "booking". I am trying to do some updates via php when a post using this CPT is saved. I have two issues that I hope you can help with.

First of all, I am hooking into save_booking_post. I am adding an action to my theme functions.php:

add_action('save_post_booking', 'save_booking_post', 20, 3);

I then have a function save_booking_post. This function is definitely being called each time a post is added or updated.

First issue I see is I want to update a field in the CPT. This field has a slug of "a0event_uuid". This field is of type "Single line".

I am seeing some strange behaviour.

my code line for updating the value of this field is:
$updateresult = update_post_meta($post_id, 'a0event-uuid', "MyuniqueID");

When a post is first created, $updateresult returns a numeric value such as 9852. I believe this indicates that the field has been updated. However, if I go to the WordPress Admin area and look at the post, the a0event-uuid field has no value (displayed).

If I now edit and update that post, save_booking_post fires again. This time, however, the value of $updateresult comes back as either true or false. Again, if I go to WordPress Admin, the field still has no displayed value.

if I add a line of code to save_booking_post to read the value of the field before updating it:

$old_value = get_post_meta($post_id, 'a0event-uuid', true);

$old_value is "" when the post is first added - this makes sense as the post did not exist before. When the post is updated $old_value returns as "MyUniqueID". So, it looks as if the code did set the value but the value is still not displaying in the WordPress Admin, or on any Front-End Forms.

Second issue is that booking has a post relationship to another CPT with a slug named "hall". The relationship is one (hall) to many (booking). Because we support anonymous users adding booking posts through front-end forms, I need to make sure that the author of the booking is set to the author of the parent "hall".

I have the following code in save_booking_post:

// Get parent "hall" and its author
$parent_hall_id = toolset_get_related_post($post_id, 'hall-booking-event', 'parent'); // replace 'hall-booking-event' with the slug of your relationship

if ($parent_hall_id) {
$parent_hall = get_post($parent_hall_id);
$parent_author = $parent_hall->post_author;

// Update 'booking' post's author if it's different from the parent 'hall's author
if ($post->post_author != $parent_author) {
$post->post_author = $parent_author;
wp_update_post($post);
}
}

This code works fine when I am editing an existing booking post. $parent_hall_id is returned with correct value. However, when the code runs as a result of adding a new booking, $parent_hall_id is always returned as "0", i.e. as if there is no parent hall for this booking. (parent hall is always selected prior to saving the new booking).

Can you advise how I can resolve these two issues.

Thanks, and best regards

Nick

#2637459

Minesh
Supporter

Languages: English (English )

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

Hello. Thank you for contacting the Toolset support.

Here is the Doc where we mentioned the following points:
- https://toolset.com/documentation/customizing-sites-using-php/updating-types-fields-using-php/

If using the standard WordPress hooks save_post or save_post_{custom-post-slug} to trigger your code you should be aware that:
-- The general save_post action must be used with a priority of at least 30 when used in conjunction with post types registered with Types, or inconsistencies in the updated data may be experienced.
-- The targeted save_post_{custom-post-slug} action is not triggered at all for post types registered with Types and should not be used.

Can you change the hook to "save_post" instead of "save_post_booking" and also make sure you use priority of the hook to trigger at least 30.

#2637469

Hi Minesh

Thanks for your response. Your advice has helped me fix the first issue. The main problem in updating the value in the field a0event-uuid was the fact that I was not using the wpcf prefix for the field name. Renaming the fieldname used in php to wpcf-a0event-uuid fixed the issue. I also switched to using save_post and gave it a priority of 30.

My second issue remains. I am trying to change the post author when a new "booking" is added. Typically this is from a front-end form and an anonymous user. The front-end form has the parent "hall" relationship on it.

When the new "booking" post is added, save_post fires and my function is called.

That function then tries to get the parent hall id using the code:

$parent_hall_id = toolset_get_related_post($post_id, 'hall-booking-event', 'parent');

However, $parent_hall is returned as "0". i.e. it looks like the "booking" is not yet associated with a "hall". This makes sense. I assume that the "booking" has to be saved and then there is some toolset code that enforces the relationship.

So my question here is what hook should I use to reliably get the id from the parent "hall" when a new "booking" post is added.

Note. My code for getting the parent hall id works fine in current function if the save_post trigger is an update to an existing booking. I just need to handle the situation when a booking is added for the first time.

Thanks

Nick

#2637475

Minesh
Supporter

Languages: English (English )

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

For frontend forms you can use hooks like "cred_save_data" or "cred_submit_complete":
- https://toolset.com/documentation/programmer-reference/cred-api/

Here is the related ticket that might help you:
- https://toolset.com/forums/topic/is-it-possible-to-set-a-default-user-author-when-someone-submits-a-content-form/#post-622822

#2637479

I can certainly try cred_save_data or cred_submit_complete when the "booking" post is being added from the front-end but I also need to run this code when a booking post is added from the back-end. This is why I was using the save_post hook.

To be honest, my issue is not the actual setting of author on the newly created "booking" post. The issue is getting the the parent "hall" post When save_post fires, it seems the "booking" post is not yet fully associated with a parent "hall" as toolset_get_related_post returns 0. I am looking for a way to reliably get the parent hall post when a new booking post is saved.

Thanks
Nick

#2637489

Minesh
Supporter

Languages: English (English )

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

What if you use the higher priority of the "save_post" hook to 99 instead of 30 and see if you able to get the parent post ID.

Does that help?

Also, you should use the remove_action('save_post', 'save_booking_post',99,3); and then add your update post function and then add the add_action('save_post', 'save_booking_post',99,3); as per the following post to avoid infinite loop.
- https://developer.wordpress.org/reference/hooks/save_post/#avoiding-infinite-loops

Here is the reference ticket.
=> https://toolset.com/forums/topic/add-auto-generate-post-title-from-fields/#post-1213248