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