Blog

Import to Advanced Custom Fields Repeater Field with Really Simple CSV Importer

Posted by admin at 4:28 日時 2014/04/18

My plugin, Really Simple CSV Importer has filter hooks. If you use these, you will be able to customize your data from csv file before importing. This blog post is an example of using filters of the importer, import data to a Repeater Field of Advanced Custom Fields (ACF) plugin. If you would like to know the detail of filter hooks, please see the Other Notes page.

Here is an example csv data.

post_status post_title textfield select text_1 text_2 text_3 num_1 num_2 num_3
publish ACF Import test Text example red,blue Repeat 1 Repeat 2 Repeat 3 100 101 102

To import to ACF Select field or ACF Repeater field, you should create array data. Usually, you can interpret array data as comma divided text or multiple columns.

Then, this is an example of filtering data. Save below code as php file, upload to wp-content/plugins/ directory, and activate it.

<?php  /*  Plugin Name: RS CSV Importer Customizer  Version: 0.1  */    function rsci_meta_filter( $meta, $post, $is_update ) {    	// Show the raw post data from csv  	echo '<pre>';  	print_r($meta);  	echo '</pre>';    	// Create containers  	$meta_array = array();  	$repeater_array = array();    	foreach ($meta as $key => $value) {    		// If custom filed name is "textfield"  		if ($key == 'textfield') {  		 	// Convert field name to ACF Field key  			$meta_array['field_52528d5b8ad30'] = $value;    		// If custom field name is "select"  		} elseif ($key == 'select') {  		 	// To import to the "Select" field, split the comma divided data to array  			$meta_array['field_52528dc88ad31'] = preg_split("/,+/", $value);    		// Create array data to import to the Repeater Field  		} elseif ($key == 'text_1') {  			$repeater_array[0]['repeater_text'] = $value;  		} elseif ($key == 'text_2') {  			$repeater_array[1]['repeater_text'] = $value;  		} elseif ($key == 'text_3') {  			$repeater_array[2]['repeater_text'] = $value;  		} elseif ($key == 'num_1') {  			$repeater_array[0]['repeater_number'] = $value;  		} elseif ($key == 'num_2') {  			$repeater_array[1]['repeater_number'] = $value;  		} elseif ($key == 'num_3') {  			$repeater_array[2]['repeater_number'] = $value;    		// Pass through normal (not ACF) custom field data  		} else {  			$meta_array[$key] = $value;    		}    	}    	// Insert Repeater data  	$meta_array['field_52528dea8ad32'] = $repeater_array;    	// Show the converted data  	echo '<pre>';  	print_r($meta_array);  	echo '</pre>';    	return $meta_array;    }  add_filter( 'really_simple_csv_importer_save_meta', 'rsci_meta_filter', 10, 3 );

Finally, you will get data as below:

Array  (      [field_52528d5b8ad30] => Text example      [field_52528dc88ad31] => Array          (              [0] => red              [1] => blue          )      [field_52528dea8ad32] => Array          (              [0] => Array                  (                      [repeater_text] => Repeat 1                      [repeater_number] => 100                  )              [1] => Array                  (                      [repeater_text] => Repeat 2                      [repeater_number] => 101                  )              [2] => Array                  (                      [repeater_text] => Repeat 3                      [repeater_number] => 102                  )          )  )

If you have a question, feel free to post Support forum. Thanks!


Share this entry