Skip to content

Instantly share code, notes, and snippets.

@jbscarbrough
Last active November 13, 2023 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jbscarbrough/9cf41d6dfabb12512cd864e834852f10 to your computer and use it in GitHub Desktop.
Save jbscarbrough/9cf41d6dfabb12512cd864e834852f10 to your computer and use it in GitHub Desktop.
Primer for importing WordPress content that includes ACF fields, using Really Simple CSV Importer plugin.
<?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