To update a repeater field created with Advanced Custom Fields programmatically you’ll need to use the update_field() function.

Populating Untouched/Unsaved Repeater Fields

The following approach will pre-populate an ACF field using the $rows array. The field ID provided in this example is repeat-field-id, replace this with your field ID when you programmatically update the field.

$rows = [
  [
    'title' => 'Something Great',
    'label' => 'Great.org',
    'url' => 'https://great.org',
  ],
  [
    'title' => 'Even Better',
    'label' => 'Better.org',
    'url' => 'https://better.org',
  ],
  [
    'title' => 'Better Still',
    'label' => 'BetterStill.com',
    'url' => 'https://betterstill.com',
  ],
];
update_field( 'repeater-field-id', $rows );

ACF provides an internal function for this, add_row(), but it doesn’t actually work if you want to inject values into a new, untouched ACF repeater field. To do this, we need to add the array directing into the field value.

Appending Values to Existing Repeater Fields

add_row() will work for adding existing rows to an already saved ACF repeater field, but it’s not an option if you want to place something in a specific spot or add and remove items at the same time. To gain this added flexibility we can use a modified version of the approach above.

$existing = get_field( 'repeater-field-id' );
if ( ! is_array($existing) ) $existing = [];

$additions = [
  [
    'title' => 'Something Great',
    'label' => 'Great.org',
    'url' => 'https://great.org',
  ],
  [
    'title' => 'Even Better',
    'label' => 'Better.org',
    'url' => 'https://better.org',
  ],
  [
    'title' => 'Better Still',
    'label' => 'BetterStill.com',
    'url' => 'https://betterstill.com',
  ],
];

$updated = $existing + $additions;

update_field( 'repeater-field-id', $updated );

This approach will add 3 new rows after any existing values. If no values exist, the 3 new rows will still be saved.

Conclusion

If you want to update a repeater field in ACF programmatically then this approach is the best way to do it. add_row() just isn’t flexible enough for most of the use cases I come across when supporting custom WordPress websites. I commonly use these approach to sync up content between staging and live environments, or my local environment and a live site.

Meet the Author

Kevin Leary, Boston Web Developer of 14 Years

I'm a freelance web developer and WordPress consultant in Boston, MA with 14 years of experience building websites and applications. View a portfolio of my work or request an estimate for your next project.