CodeWP
Tagged:

How to: ACF Bulk data import for repeaters (ACF v6+)

By James LePage, CodeWP Founder
January 31, 2023

Earlier, I was tying to import a bulk list of links into an ACF repeater. There are several heavy import plugins that will get the job done, but I needed something simpler.

I used CodeWP to generate this code for my use case. When saving a specific post type, it'll check if I've added data to import, and if it's present, add it to the specified repeater. Quick and simple!

//when post type is dataset and it's updated, run this function (only on the admin backend post edit page) add_action('acf/save_post', 'urlimporter', 20); function urlimporter() { //if import_links acf field has content, and it's comma separated (https://url1.com,https://url2.com), get each item //for each link (use foreach), create a new repeater row for 'dataset' repeater, then add the link to the 'original_link' subfield //on success, delete the import_links field if (get_field('import_links')) { $post_id = get_the_ID(); $repeater_name = 'dataset'; $import_links = get_field('import_links'); $import_links = explode(",", $import_links); foreach ($import_links as $link) { add_row($repeater_name, array('original_link' => $link), $post_id); } update_field('import_links', '', $post_id); } }

Here's how it works.

We have two fields set using ACF for our custom post type. Import links, and the repeater.

If the user saves the post, and there is a common separated, list of links in the import links field, the function will run separating out each link and then adding it as a new repeater role. It will also clear the field.

It uses the acf/save_post action, which fires when saving the submitted $_POST data.

This function is a quick and simple way to insert large amounts of data into repeaters and it's easily extendable using CodeWP.

Snippets In This Tutorial Were Generated With CodeWP
php
PHP, JS or jQuery
select_all
AI models trained for WordPress & popular plugins
switch_access_shortcut
Unlimited generations
card_giftcard
100% free trial
Start For Free