update_sub_row()


目录

Updates a row of data for an existing Repeater or Flexible Content sub field value.

This function can be used inside or outside of a have_rows() loop. When used inside, the current row will be used to update the sub field value. When used outside, the rows and parents must be specified to target the correct value place.

Please note this function is used to modify a sub field value. To modify a parent field value, please use the update_row() function.

Parameters

update_sub_row($selector, $row, $value, [$post_id])

Return

(bool) True on successful update, false on failure.

Change Log

Update a sub row inside of a have_rows() loop

This example shows how to update a row of data to an existing nested repeater field called ‘images’. This nested repeater field contains 3 sub fields (‘image’, ‘alt’, ‘link’).

// Loop over all staff members.
if( have_rows('staff_members') ) {
    while( have_rows('staff_members') ) {
        the_row();

        // Get this staff member's name.
        $name = get_sub_field('name');

        // Update this staff member's first image.
        $value = array(
            'image' => 123,
            'alt'   => "The first photo of $name",
            'link'  => 'http://website.com'
        );
        update_sub_row('images', 1, $value);
    }
}

Update a sub row outside of a have_rows() loop

This example shows how to update a row of data outside of a have_rows() loop. Here, the $selector parameter is provided an array containing a mixture of field names and row numbers. This array should read from left to right, the parents to children relationship separated by the row number.

Please see notes regarding index offset.

// Update the first staff member's first image.
$value = array(
    'image' => 123,
    'alt'   => "The first photo of the first staff member",
    'link'  => 'http://website.com'
);
update_sub_row( array('staff_members', 1, 'images'), 1, $value );

// Update the first staff member's second image.
update_sub_row( array('staff_members', 1, 'images'), 2, $value );

Notes

Index offset

When targeting a sub field using a specific row number, please note that row numbers begin from 1 and not 0. This means that the first row has an index of 1, the second row has an index of 2, and so on.

To begin indexes from 0, please use the row_index_offset setting like so.

functions.php

add_filter('acf/settings/row_index_offset', '__return_zero');

版本号 #1
由 壳壳虫 创建于 2026-08-29 15:44:03 UTC
由 壳壳虫 更新于 2026-08-29 15:44:03 UTC