Support Forum
Support › Meta Box Group › Group field not updating properly on save
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'.
Hello,
Supporting a customization code for your specific needs is beyond our scope of support, please read more here https://support.metabox.io/topic/support-policy/
I think you can move the code that updates the group field value outside of the loop and recheck this issue. If you are not able to complete the task, please contact us here https://metabox.io/contact/
We offer a customization service with an extra fee.