Migrate an ACF Repeater Into Its Own Custom Post Type
Hey there so here is another fairly complex task where in you would be given the task of migrating Advanced Custom Fields PRO (ACF) repeater object into its own custom post type within WordPress.

Why Would You Get This Problem?

Sometimes when developers build the websites using ACF they might want to consider using groups or repeaters to be displayed within a section. Think of like looping all of these repeating elements within a page, only realising that later on the requirements have changed to display this onto another page and another and another.As such you might not even want to repeater.In fact sometimes using a repeater can be a mistake. You might want to use that as a custom post type rather than as a repeater.So here is one solution where in you can make that turning point from a repeater found in one page, towards a custom post type.

The Code

 * Migrate an ACF Repeater Into Its Own Custom Post Type
 * @author Michael Sumner
 * @link https://smnr.co/codeable
function migrate_an_acf_repeater_to_its_own_cpt( $post_id ) {

    // get the ACF repeater object named as `events`
    // id 1234 is an example of one of the page IDs containing this ACF repeater object
    $events = get_field_object( 'events', 1234 );

	// get the cpt_slug
	$cpt_slug_ids = get_posts( array(
		'fields'         => 'ids',
		'posts_per_page' => -1,
		'post_type'      => 'cpt_slug',
	) );

    foreach ($events['value'] as $event): 

		foreach ($cpt_slug_ids as $cpt_slug_id): 

            if( get_the_title( $cpt_slug_id ) === $event['event_title'] ): 
                // vars
				$event_title       = $event['event_title'];
				$event_image       = $event['event_image'];
				$event_description = $event['event_description'];
				$event_link        = $event['event_link'];

				$row = array(
					'event_title'       => $event_title,
					'event_image'       => $event_image,
					'event_description' => $event_description,
					'event_link'        => $event_link,

                add_row('events', $row, $cpt_slug_id);



add_action( 'acf/save_post', 'migrate_an_acf_repeater_to_its_own_cpt' );

The function consists of an action hook called acf/save_post meaning that we want to run this function whilst we are saving the post.

We pick ID containing the repeater — in this example 1234 and prepare the migration by retrieving all the custom post types in this example called cpt_slug. Now let us loop through every CPT and add the ACF row within the repeater based on the CPT having the same title as one of the event_title fields within that repeater row.

We need something that matches (the most of obvious of which is the title) so that we can know when to skip to the next CPT. Once we are done migrating one repeater row matched with its accompanying CPT, we move onto the next repeater row, until all repeater rows have been processed.

To initiate this migration, you would simply have to add this function, and save any post within WordPress.

Of course, to improve upon this, there are more resilient ways to accomplish the challenge.