Skip to content

Instantly share code, notes, and snippets.

@geoffyuen
Created November 22, 2018 15:56
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save geoffyuen/d3e9154db842aba9cd3e9e61523c4028 to your computer and use it in GitHub Desktop.
Save geoffyuen/d3e9154db842aba9cd3e9e61523c4028 to your computer and use it in GitHub Desktop.
Import CSV into ACF Repeater (Wordpress)

Via: https://felicoz.com/en/2017/05/acf-import-repeater-field-values-csv-programmatically/

  1. Prepare your CSV file with columns matched to the repeater sub fields
  2. ACF – create a csv file select field
  3. ACF – set up the repeater field with sub fields
  4. create a new post
  5. From the post, upload your csv
  6. create empty repeater fields depending on the number of rows you have in csv
  7. In the front page, paste this php code:
<?php
if(get_field('csv_file'))
{
  // load csv with SERVER PATH instead of URL
  $csv = get_attached_file(get_field('csv_file')['id']); 
  if(($handle = fopen($csv, "r")) !== FALSE)
  {
    $count=0;
    while(($data = fgetcsv($handle, 1000, ",")) !== FALSE)
    {
      // $count = acf row, $data[0] = csv column 1 value
      update_sub_field(array('repeater_name', $count, 'name'), $data[0]);
      update_sub_field(array('repeater_name', $count, 'address'), $data[1]);
      update_sub_field(array('repeater_name', $count, 'phone'), $data[2]);
      $count++;
    }
    fclose($handle);
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment