Hi, hope you can help with a little bit of a weird problem.
I have a CPT called 'events', which is programmatically updated when a 'producer' makes a booking for the event. Additions to the event are working fine, but if someone makes a cancellation for the event (after having previously been booked), the CPT does not update properly.
The 'producer' is removed from the event (which is a group field), but the event does not appear to save properly. On the front-end, the only producers shown for the event are the ones before the deleted producer in the group.
If I go in to the backend and manually 'update' the event (not making any changes, just updating to resave), something happens and the event is saved correctly, and all producers for the event are displayed on the frontend.
Here's part of the code, highlighting the delete code:
function update_event_producer_field( $post_id ) {
$booking_events = rwmb_meta( 'events_to_attend', array(), $post_id );
$author_id = get_post_field( 'post_author', $post_id );
$producer_booking_id = rwmb_meta( 'producer_id', [ 'object_type' => 'user' ], $author_id );
$delete_events = rwmb_meta( 'events_to_delete', array(), $post_id );
if ( $delete_events ) {
error_log( 'In delete code' );
foreach ( $delete_events as $event_id ) {
$event_producers = rwmb_meta( 'producers_attending', array(), $event_id );
foreach ( $event_producers as $key => $producer ) {
if ( $producer['producer_id'] === $producer_booking_id ) {
unset( $event_producers[ $key ] );
error_log( 'In delete loop' );
update_post_meta( $event_id, 'producers_attending', $event_producers );
wp_update_post(array( 'ID' => $event_id, ));
}
}
}
}
}
add_action( 'save_post', 'update_event_producer_field', 20 );
How I can programmatically force it to properly save the event so I do not have to go in and manually 'update' it?
The second image is the CPT with the group field. Note there are entries after 'Angels Bakery'.
The third image is what is shown on the front-end. Note there are no entries shown after 'Angels Bakery'.