How append new rows to an existing repeater field

Created by Phil Kurth, Modified on Fri, 23 Dec 2022 at 10:18 AM by Phil Kurth

When using in an ACF form with repeater fields to edit a post or user, you might find that the repeater grows significantly over time creating an unfriendly user experience. Paginated repeaters — a feature introduced in ACF 6.0 – is deactivated on front end forms so we can't use this feature to simplify our forms. Instead, you may choose to stop mapping the repeater field on form submission and instead use a custom submission handler to add new rows to the existing repeater on form save. This will result in an empty repeater field when rendering the form but when saved, the data is appended to the existing repeater.


First, ensure the repeater field is not mapped in the form settings. We'll be handling the saving of this data manually.



Next, modify the following snippet to suit your situation and add it to your functions.php file or a custom plugin. This saves the incoming repeater field rows as a new row on form submission:


<?php

add_action( 'af/form/submission', function ( $form, $fields, $args ) {

  // If we need to run this only on a specific form, check for the form key.
//  if ( $form['key'] !== 'YOUR_FORM_KEY_HERE' ) {
//    return;
//  }

  // Get the incoming repeater field values
  $items = af_get_field( 'items' );

  // Iterate over all the rows
  foreach ( $items as $item ) {

    // Get the ID to save the values against. If updating a post, this will be the post ID in the form's settings.
    $id = AF()->submission['args']['post'];

    // If updating a user, this will be the user ID in the form's settings but ACF will need the `user_` prefix.
    //$id = 'user_' . AF()->submission['args']['user'];

    // Use ACF's add_row() function to append the inbound data to the desired repeater field for the edited post.
    add_row( 'items', $item, $id );
  }
}, 10, 3 );

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article