Support

Account

Home Forums ACF PRO Updating Repeater Fields: update_field() vs. add_row()

Solving

Updating Repeater Fields: update_field() vs. add_row()

  • I am trying to dynamically update a repeater field from a template in the front end of my site.

    My current code is as follows:

      // save a repeater field value
      $regions = [] // array of 0 to many regions (populated dynamically, tested working)
      $sectors = [] // array of 0 to many sectors
      $field_key = "field_5c4b34657ab9a"; // repeater field
      $value = array(
          array(
              'field_5c4b34917ab9b'	=> implode(', ', $regions), // text area (sub field)
              'field_5c4b34c87ab9c'	=> implode(', ', $sectors), // text area (sub field)
              'field_5c4b34d17ab9d' => date('Y-m-d H:i:s', time()) // text (sub field)
          )
      );
      add_row($field_key, $value, $pressReleaseID);
      update_field($field_key, $value, $pressReleaseID);
    

    When I use ‘update_field()’ alone, each time I run the code, it will write the correct values but each new repeater row entry will overwrite the previous row. When you check the post in the backend, there will be one row on the repeater filled out, and it will be the last entry you tried to save.

    If I use ‘add_row()’ alone, each time I run the code, it will create a blank repeater row, and the values will be lost. If I run it 3 times, in the back end there will be 3 blank rows on the repeater field.

    If I use both ‘add_row()’ and then ‘update_field()’, I seem to get the same result as if I had just used ‘update_field()’ alone (I thought perhaps I could use add_row() to add a blank row first, and then update_field() to update the blank row. Apparently not.

    The documentation doesn’t seem to cover this.

    What I need to do is save a new row on the repeater field dynamically.

    i.e. If I run this code 5 times, I should get 5 new rows added to the repeater field, with the relevant values.

    I have tried using field keys and field names, every combination I can think of.

    What is the correct way to save new repeater field rows programmatically? (that aren’t blank!).

  • When you use update_field() you would need to include all of the existing values along with the new values. This means that you need to first get all the existing values and then add a new row to it and then use update_field().

    When using add_row(), the value you’re using is incorrect. For update_field() it should be the nested array that you have, each row being nested in the repeater. However, when updating a single row this array should not be nested.

    
    
      $value = 
          array(
              'field_5c4b34917ab9b'	=> implode(', ', $regions), // text area (sub field)
              'field_5c4b34c87ab9c'	=> implode(', ', $sectors), // text area (sub field)
              'field_5c4b34d17ab9d' => date('Y-m-d H:i:s', time()) // text (sub field)
      );
      add_row($field_key, $value, $pressReleaseID);
    
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Updating Repeater Fields: update_field() vs. add_row()’ is closed to new replies.