Support

Account

Home Forums ACF PRO Adding data into a repeater field.

Helping

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:

    <?php
    
    function readCSV($csvFile)
    {
        $file_handle = fopen($csvFile, 'r');
        fgetcsv($file_handle, 1024, ",");
        while (!feof($file_handle)) {
            $line_of_text[] = fgetcsv($file_handle, 1024);
        }
        fclose($file_handle);
    
        return $line_of_text;
    }
    
    $csvFile = 'data/dtas-inventory.csv';
    
    $inventory = readCSV($csvFile);
    
    $field_keys = array(
        'field_58c95667b0f7d',
        'field_58c956c86747a',
        'field_58c9576a1f775',
        'field_58c957951f776',
        'field_58c9581639a32',
        'field_58c9582739a33',
        'field_58c9583339a34',
        'field_58c9583d39a35',
        'field_58cc131c5f7ac',
        'field_58c958ad3dae4',
        'field_58c958b53dae5',
        'field_58c958bb3dae6',
        'field_58c958c63dae7',
        'field_58c958dc942c8',
        'field_58cc138e09213',
        'field_58c958f1942c9',
        'field_58cc13ba6995a',
        'field_58c95901942ca',
        'field_58c959189521f',
        'field_58c9592195220',
        'field_58c9592795221',
        'field_58c9593195222',
        'field_58c9594495223',
        'field_58c9596595224',
        'field_58c959dd2451a',
        'field_58cc13f66995b',
        'field_58cc140c6995c',
        'field_58cc141c6995d',
        'field_58c95a102451b',
        'field_58cc142b6995e',
        'field_58cc143b6995f',
        'field_58cc145169960',
        'field_58c95a2d3ded4',
        'field_58cc146269961',
        'field_58cc146e69962',
        'field_58cc147869963'
    );
    
    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

    
    array(
      // each row of the repeater is a nested array
      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
      array(
        // 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.