Home Forums ACF PRO Adding data into a repeater field.


Adding data into a repeater field.

  • I got some amazing help from the following previous thread.

    Long story short, I have a CSV file that I am loading into custom fields. Here is my code which works like a charm:

    function readCSV($csvFile)
        $file_handle = fopen($csvFile, 'r');
        fgetcsv($file_handle, 1024, ",");
        while (!feof($file_handle)) {
            $line_of_text[] = fgetcsv($file_handle, 1024);
        return $line_of_text;
    $csvFile = 'data/dtas-inventory.csv';
    $inventory = readCSV($csvFile);
    $field_keys = array(
    for ($i = 0, $numInventory = count($inventory); $i < $numInventory; $i++) {
        if ($inventory[$i] != 0){
            $post = wp_insert_post(array(
                'post_title'  => 'Vehicle ' . $i,
                'post_type'   => 'vehicle',
                'post_status' => 'publish'
            for ($m = 0, $numSub = count($inventory[$i]); $m < $numSub; $m++) {
                update_field($field_keys[$m], $inventory[$i][$m], $post);

    I just purchased PRO and changed two of my custom fields to repeater fields (one of the CSV cells contains comma separated data of different vehicle options, and another cell is a comma separated list of image urls). I figured a repeater field that listed these 2 sets individually would be best. I’m having a hard time figuring out where to start. I think that I would need to check for the repeater fields in my second for loop that calls the update_field() function, but I’m not sure. All of my attempts at getting it to work end in the following warning:

    PHP Warning: Invalid argument supplied for foreach() in ......./plugins/advanced-custom-fields-pro/pro/fields/repeater.php

    I’m not looking to have the answer given to me, but I’ve been looking through all of the documentation trying different combination of update_sub_field(), add_sub_row(), and update_field() with no success. Could someone just point me in the right direction? Thanks!

  • You need to organize your repeater entries into rows, with that done you can call update_field() using the array. Here’s an example of what you need to use as the value for update field when dealing with a repeater

      // each row of the repeater is a nested array
        // each element of the row array is a subfield value
        'field_123456' => 'field 1 value',
        'field_234567' => 'field 2 value',
      ), // end of first row
        // each element of the row array is a subfield value
        'field_123456' => 'field 1 value',
        'field_234567' => 'field 2 value',
      ), // end of second row
    ) // end of repeater

    Let me know if that helps at all

Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Adding data into a repeater field.’ is closed to new replies.