I have group fields inside repeater field. I want to update the value of specific group field value programmatically.
Example - Below is an array of repeater field.
Array
(
[0] => Array
(
[booking_list] => Array
(
[termin] => November 20, 2021 12:00 am
[available_seats] => 5
)
)
[1] => Array
(
[booking_list] => Array
(
[termin] => November 30, 2021 12:00 am
[available_seats] => 6
)
)
)
**I want to update the value of available_seats field
Edited -: I tried this code but not working.
if( have_rows('booking') ) {
$i = 0;
while( have_rows('booking') ) {
the_row();
$i ;
if(have_rows('booking_list')){
while( have_rows('booking_list') ){
the_row();
update_sub_field('available_seats', 2);
}
}
}
}
Screenshot Click Me
CodePudding user response:
You can use the update_field
also. You just need to see the meta-key
in the database. It is usually stored like this: parent_booking_list_available_seats
. Of course not "parent" you need to have a look in the database.
CodePudding user response:
My personal approach is to use get_field
and since it returns an array, you can just loop through different items and update the fields where necessary.
Below snippet would make all available_seats to 2. You can introduce conditional logic if needed.
$post_id = get_the_ID();
$bookings = get_field( 'booking', $post_id );
foreach ( $bookings as &$booking ) {
foreach ( $booking['booking_list'] as &$booking_item ) {
$booking_item['available_seats'] = 2;
}
}
update_field( 'booking', $booking, $post_id );