add_row()

Overview

This function will add a new row of data to an existing repeater field field value.

Change Log

  • Added in version 5.3.2

Parameters

add_row( $selector, $value, $post_id )
  • $selector: (required) The parent field name or key
  • $value: (required) The new value to append
  • $post_id: (optional) The post ID of which the value is saved to. Defaults to current post

Return

This function will return the new count of rows or false upon failure.

Examples

Basic

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

<?php 

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

$i = add_row('images', $row);
	
?>

Basic (using keys)

This example demonstrates how to add a new row of data to an existing repeater field using fields instead of names. The repeater field is the same above.

Similar to the update_field() function, using a field’s key rather than it’s name allows ACF to correctly find the field if no existing value has been saved. To expand on this, if no repeater field value exists, this function will fail unless field key’s are used.

<?php 

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

$i = add_row('field_560389746a51f', $row);
	
?>

 

Related