-
-
Save coachben/a6a937250403f11f074bc11e5c6a86c9 to your computer and use it in GitHub Desktop.
Primer for importing WordPress content that includes ACF fields, using Really Simple CSV Importer plugin.
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 characters
<?php | |
// Import function | |
function custom_csv_import($meta, $post, $is_update) { | |
// Create containers | |
$meta_array = array(); | |
$repeater_array = array(); | |
/* It may help to list out the ACF fields you are populating | |
Table Heading ACF Key Repeater Fields | |
============= ================== ================= | |
sample_field field_XXXXXXXXXXXX N/A | |
sample_field field_XXXXXXXXXXXX N/A | |
sample_field field_XXXXXXXXXXXX ['link_type'], ['link_text'], ['link_url'] | |
*/ | |
// Loop through CSV | |
foreach ($meta as $key => $value) { | |
if($key == '**YOUR_TABLE_HEADING**') | |
{ | |
// Example of comma separated list (ACF select, check-box, etc.) - run before other fields | |
$meta_array['**YOUR_ACF_FIELD_KEY**'] = preg_split("/,+/", $value); | |
} | |
elseif($key == '**YOUR_TABLE_HEADING**') | |
{ | |
// Example of standard field | |
$meta_array['**YOUR_ACF_FIELD_KEY**'] = $value; | |
} | |
elseif($key == '**YOUR_REPEATER_FIELD_HEADING__1.1**') | |
{ | |
// If there is a value, add value to a repeater array (with field name as key) | |
($value) ? $repeater_array[0]['**YOUR_REPEATER_FIELD_NAME**'] = $value : false; | |
} | |
elseif($key == '**YOUR_REPEATER_FIELD_HEADING__1.2**') | |
{ | |
// If there is a value, add value to a repeater array (with field name as key) | |
($value) ? $repeater_array[0]['**YOUR_REPEATER_FIELD_NAME**'] = $value : false; | |
} | |
elseif($key == '**YOUR_REPEATER_FIELD_HEADING__2.1**') | |
{ | |
// If there is a value, add value to a repeater array (with field name as key) | |
($value) ? $repeater_array[0]['**YOUR_REPEATER_FIELD_NAME**'] = $value : false; | |
} | |
elseif($key == '**YOUR_REPEATER_FIELD_HEADING__2.2**') | |
{ | |
// If there is a value, add value to a repeater array (with field name as key) | |
($value) ? $repeater_array[0]['**YOUR_REPEATER_FIELD_NAME**'] = $value : false; | |
} | |
else | |
{ | |
// For basic fields(like post_title) run standard import | |
$meta_array[$key] = $value; | |
} | |
} | |
// Insert Repeater data | |
$meta_array['**YOUR_REPEATER_ACF_KEY**'] = $repeater_array; | |
return $meta_array; | |
} | |
// Hook into really_simple_csv | |
add_filter('really_simple_csv_importer_save_meta', 'custom_csv_import', 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment