Skip to content

Instantly share code, notes, and snippets.

@mariohercules
Created October 2, 2020 12:12
Show Gist options
  • Save mariohercules/cd651e911dc999e02e4edbd29bce43bc to your computer and use it in GitHub Desktop.
Save mariohercules/cd651e911dc999e02e4edbd29bce43bc 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