Support

Account

Home Forums Add-ons Repeater Field Converting Repeater Rows into Posts Reply To: Converting Repeater Rows into Posts

  • 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!