简体   繁体   English

以编程方式更新转发器字段中特定组字段的值 - 高级自定义字段 (ACF) - Wordpress

[英]Update the value of specific group field inside repeater field programmatically - Advanced Custom Field (ACF) - Wordpress

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 **我想更新 available_seats 字段的值

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截图点我

You can use the update_field also.您也可以使用update_field You just need to see the meta-key in the database.您只需要查看数据库中的meta-key It is usually stored like this: parent_booking_list_available_seats .它通常这样存储: parent_booking_list_available_seats Of course not "parent" you need to have a look in the database.当然不是“父母”,您需要查看数据库。

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.我个人的方法是使用get_field ,因为它返回一个数组,所以您可以遍历不同的项目并在必要时更新字段。 Below snippet would make all available_seats to 2. You can introduce conditional logic if needed.下面的代码片段将使所有 available_seats 变为 2。如果需要,您可以引入条件逻辑。

$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 );

If you Still need an answer.如果您仍然需要答案。 This will update a grouped field inside a repeater field.这将更新转发器字段内的分组字段。 Repeater > Group > Group Field 1中继器 > 组 > 组字段 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 @kamal

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM