Created
November 22, 2018 15:56
-
-
Save geoffyuen/d3e9154db842aba9cd3e9e61523c4028 to your computer and use it in GitHub Desktop.
Revisions
-
geoffyuen created this gist
Nov 22, 2018 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,31 @@ 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); } } ```