[英]Update the value of specific group field inside repeater field programmatically - Advanced Custom Field (ACF) - Wordpress
我在轉發器字段中有組字段。 我想以編程方式更新特定組字段值的值。
示例 - 下面是一個中繼器字段數組。
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
)
)
)
**我想更新 available_seats 字段的值
已編輯-:我嘗試了此代碼但無法正常工作。
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);
}
}
}
}
截圖點我
您也可以使用update_field
。 您只需要查看數據庫中的meta-key
。 它通常這樣存儲: parent_booking_list_available_seats
。 當然不是“父母”,您需要查看數據庫。
我個人的方法是使用get_field
,因為它返回一個數組,所以您可以遍歷不同的項目並在必要時更新字段。 下面的代碼片段將使所有 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 );
如果您仍然需要答案。 這將更新轉發器字段內的分組字段。 中繼器 > 組 > 組字段 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);
}
我就是這樣做到的。 希望對你有幫助! @kamal
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.