skip to Main Content

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

3

Answers


  1. 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.

    Login or Signup to reply.
  2. 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 );
    
    Login or Signup to reply.
  3. If you Still need an answer.
    This will update a grouped field inside a repeater field.
    Repeater > Group > Group Field 1

     $repeater = get_field('prepeater_field',$post_id);
       if(count($repeater) > 0){
          $or = 0;
            foreach($repeater as $rfield){
                $ir = 0;
                foreach($rfield as $fields){
                  
                  $repeater[$or]["group_field_main"]["group_sub_field"] = 'Your new value';
                     
                  $ir++;
                }
                $or++;
            }
       
            update_field('prepeater_field',$repeater,$id);
        
       }
    

    That’s how I made it. Hope this will help you! @kamal

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search