add_row()ACF 5.2.3

Adds a new data row to the existing value of a Repeater or Flexible Content field.

No Hooks.

Returns

(boolean). The updated row count, or false on error.

Usage

add_row( $selector, $value, $post_id )
$selector(string) (required)
The field name or key.
$value(array)
The row containing new data.
Default: false
$post_id(integer/mixed)
The post ID in which to save the data.
Default: false (the current post)

Examples

#1 Add a new row using field names

This example shows how to add a new data row to an existing image repeater field. This field contains three subfields: image, alt, and link.

$row = [
	'image' => 123,
	'alt'   => 'Another great sunset',
	'link'  => 'http://website.com'
];

add_row( 'images', $row );

#2 Add a new row using field keys

This example shows how to add a new data row to an existing Repeater field using keys instead of names. The Repeater field is the same as in the example above.

Like update_field(), using the field key instead of its name allows ACF to find the field correctly when the saved value does not exist.

$row = [
	'field_560389746a525' => 123,
	'field_560389746a524' => 'Another great sunset',
	'field_560389746a528' => 'http://website.com'
];

add_row( 'field_560389746a51f', $row );

Changelog

Since 5.2.3 Introduced.

add_row() code ACF 6.8.8

function add_row( $selector, $row = false, $post_id = false ) {

	// filter post_id
	$post_id = acf_get_valid_post_id( $post_id );

	// get field
	$field = acf_maybe_get_field( $selector, $post_id, false );

	// bail early if no field
	if ( ! $field ) {
		return false;
	}

	// get raw value
	$value = acf_get_value( $post_id, $field );

	// ensure array
	$value = acf_get_array( $value );

	// append
	$value[] = $row;

	// Paginated repeaters should be saved normally.
	$field['pagination'] = false;

	// update value
	acf_update_value( $value, $post_id, $field );

	// return
	return count( $value );
}