Home Forums ACF PRO Repeater data from multiple Custom Fields in a Custom Post Type


Repeater data from multiple Custom Fields in a Custom Post Type

  • I have an imported CSV file that has multiple “duplicate” rows of data. There are 855 rows and some of the row “titles” repeat in the CSV, but they contain different data in them. (these are course schedules). For Example: There are 4 rows with AMT 100 as the field I am using for the title. Then each of those AMT 100 rows have different data for that course schedule slot. I have gotten to the point where I am able to combine those duplicate entries into 1 single entry for each cpt with the same “identifier”. However, when I go to the update the repeater field, it only fills in 1 repeater for the new post instead of the 3 repeated rows of data, inside the single post. So essentially, if there are 3 AMT 100 courses, it would have this setup. (Normally I could manually change things, but this data-set has 855 rows..)
    AMT 100 (all other rows that have this same data string are combined into 1)
    – repeater data 1 (from another AMT 100 course)
    – repeater data 2 (from another AMT 100 course)
    – and so on for however many “duplicate” AMT 100’s there are.

    And do that for all 25 courses.

    Code So far (this is run via a batch process): The comments in the code may be more helpful than my explanation 🙂

    function edit_all_imported_courses() {
    	register_batch_process( array(
    		'name' => 'Edit Imported Course Schedules',
    		'type' => 'post',
    		'args' => array(
    			'post_type' => 'importer',
    			'posts_per_page' => 1,
    			'orderby' => 'post_title'
    		'callback' => 'edit_imported_courses',
    	) );
    add_action( 'locomotive_init', 'edit_all_imported_courses' );
    function edit_imported_courses( $imported_row ) {
    	global $wpdb;
    	// this is what we want to look for to identify "duplicates" (courses with the same number/subject but different dates/times)
    	$identifier = get_post_meta( $imported_row->ID, 'course_identifier', true );
    	// get all the post meta in an array format - usage:   $imported_meta[$key][0]
    	$imported_meta = get_post_meta( $imported_row->ID );
    	// repeater field key
    	$field_key = 'field_5cd58dcbbcc8c';
    	// repeater data - all below is variable info per course
    	$field_value = [[
    		'field_5cd58e25bcc8d' => $imported_meta['semester'][0],
    		'field_5cd58e87fd636' => $imported_meta['course_section'][0],
    		'field_5cd58e97fd637' => $imported_meta['class_schedule_slot'][0],
    		'field_5cd58eacfd638' => $imported_meta['primary_location'][0],
    		'field_5cd58ebafd639' => $imported_meta['building'][0],
    		'field_5cd58ec8fd63a' => $imported_meta['room'][0],
    		'field_5cd58edb14e2a' => $imported_meta['monday'][0],
    		'field_5cd58eea14e2b' => $imported_meta['tuesday'][0],
    		'field_5cd58ef214e2c' => $imported_meta['wednesday'][0],
    		'field_5cd58efc14e2d' => $imported_meta['thursday'][0],
    		'field_5cd58f0714e2e' => $imported_meta['friday'][0],
    		'field_5cd58f0e14e2f' => $imported_meta['saturday'][0],
    		'field_5cd58f1a14e30' => $imported_meta['sunday'][0],
    		'field_5cd58f2e1667d' => $imported_meta['class_start_time'][0],
    		'field_5cd58f3d1667e' => $imported_meta['class_end_time'][0],
    		'field_5cd58f461667f' => $imported_meta['class_start_date'][0],
    		'field_5cd58f5316680' => $imported_meta['class_end_date'][0]
    	// find post where meta_key => value is 'course_identifier' => "$identifier"
    	$existing_post = (new WP_Query( [
    		'post_type' => 'course',
    		'meta_key' => 'course_identifier',
    		'meta_value' => $identifier,
    		'posts_per_page' => 1
    	] ) )->get_posts();
    	// finds duplicates and combines them
    	if ( ! empty($existing_post) ) {
    		$existing_post = $existing_post[0];
    		$id = $existing_post->ID;
    	} else {
    		$id = wp_insert_post( ['post_type' => 'course', 'post_title' => $identifier, 'post_content' => '', 'post_status' => 'publish'] );
    		update_post_meta( $id, 'course_identifier', $identifier );
    		update_field( 'field_5cd58cd4bcc89', $imported_meta['year'][0], $id );
    		update_field( 'field_5cd59978909b4', $imported_meta['course_identifier'][0], $id );
    		update_field( 'field_5cd58e33bcc8e', $imported_meta['course_subject'][0], $id );
    		update_field( 'field_5cd58e3fbcc8f', $imported_meta['course_number'][0], $id );
    	update_field( $field_key, $field_value, $id );

    The data creates a single Repeater row for the data, but does not create more repeaters for each duplicate. I know I probably need a foreach() loop, but i’m not 100% sure how to go about getting this data to repeat inside it.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.