Insert new row in ACF repeater under a group using PHP
I was looking for a solution to easily insert a new row into an ACF repeater field inside a group. Figured I would share the working PHP code snippet, since I didn’t find much that was simple or clean.
Here is the working code:
// Check if ACF plugin is active
if( !function_exists('acf_add_local_field_group') ) {
die('ACF plugin is not active.');
}
// Get the post meta of the specified post
$repeater_field_name = get_field('repeater_field_name ', $post_id);
// Create a new row array
$new_row = array(
'repeater_subfield_1' => $repeater_subfield_1_value,
'repeater_subfield_2' => $repeater_subfield_2_value
);
// Add the new row to the repeater field
$repeater_field_name[] = $new_row;
// Update the ACF field with the new data
update_field('repeater_field_name', $repeater_field_name = get_field('repeater_field_name ', $post_id);
This code worked for me, and is tested.