Home › Forums › Add-ons › Repeater Field › Converting Repeater Rows into Posts
Hi guys, a bit of an odd one here — I need to convert about 300,000 repeater rows, spread across thousands of posts (in three different Custom Post Types), into their own separate Posts.
We’re migrating data from an old site to a new one, and as part of that we need to recreate these entries into their own Custom Post Type. So far, I’ve not had much luck.
I’m currently looking at ways to reformat the exported SQL for these particular entries, and use that to enter them as posts, but I feel like there’s a lot of work ahead if we go that route.
Has anybody else done anything similar to this before? I couldn’t find too much online.
Thank you!
Whoa, that’s a pretty mammoth task!
My approach would be to make use of the API and batch processing.
The idea being that it can work through without (in theory) timing out.
You would need to handle that for each custom post type. I assume it’s the same repeater for each post type?
As a possible starting point, perhaps something like the below:
<?php
add_action( 'wp_ajax_nopriv_get_batch_posts_from_api', 'get_batch_posts_from_api' );
add_action( 'wp_ajax_get_batch_posts_from_api', 'get_batch_posts_from_api' );
function get_batch_posts_from_api() {
$current_page = ( ! empty( $_POST['current_page'] ) ) ? $_POST['current_page'] : 1;
// create a file to test the output (this will be the same dir as your functions file if you add this code to functions.php)
$page_report = get_stylesheet_directory() . '/page_report_'.date("dmY").'.txt';
$data = 'Current Page: '.$current_page;
file_put_contents( $page_report, $data . "\n", FILE_APPEND ); // useful to debug
// Create an array
$posts = [];
// $results return an array of objects - CHANGE /posts/ to your custom post type
// Go to the URL https://www.domain.com/wp-json/wp/v2/posts/ to check it shows posts (amend URL accordingly)
$results = wp_remote_retrieve_body( wp_remote_get('https://www.domain.com/wp-json/wp/v2/posts/?page=' . $current_page . '&per_page=10') );
// convert JSON string to PHP array
$results = json_decode( $results );
// either the API is unavailable or we have an error, so bail
if( ! is_array( $results ) || empty( $results ) ){
return false;
}
$posts[] = $results;
foreach( $posts[0] as $post ){
if( have_rows('repeater_field_name', $post->id) ):
while( have_rows('repeater_field_name', $post->id) ) : the_row();
$sub_value_1 = get_sub_field('sub_field_1');
$sub_value_2 = get_sub_field('sub_field_2');
// Create post object
$post_title = wp_strip_all_tags( $sub_value_1 );
$new_post = array(
'post_title' => $post_title,
'post_content' => $sub_value_2,
'post_status' => 'publish',
'post_author' => 1,
);
// Insert the post into the database
$post_id = post_exists( $post_title ) or wp_insert_post( $new_post );
endwhile;
endif;
}
// browse to the URL to trigger this script
// https://www.domain.com/wp-admin/admin-ajax.php?action=get_batch_posts_from_api
$current_page = $current_page + 1;
wp_remote_post( admin_url('admin-ajax.php?action=get_batch_posts_from_api'), [
'blocking' => false,
'sslverify' => false, // we are sending this to ourselves, so trust it.
'body' => [
'current_page' => $current_page
]
]);
}
The code works for batch processing but clearly not tested for your needs! So may require tinkering.
I assume the CPT is accessible via the API (check by browsing to the API URL).
I also assume the repeater is the same in each post
Finally, you would need to map the repeater fields to the new post fields.
Hope that helps!
Thank you so much for your help on this – that was a pretty helpful starting point!!
Do you think you could point me in the right direction as far as mapping the repeater fields goes? Would I have to use update_field()
?
It depends what the repeater currently includes and how you need to use the repeater fields in the new posts as to how you map the data!
Does the newly created post not have a repeater and all existing repeater fields go to new custom fields for example?
If so, you can use update_post_meta()
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.