Support

Account

Home Forums Bug Reports update_sub_field updating different row

Unread

update_sub_field updating different row

  • I have a task where I’m trying to sync a repeater sub-field value on one custom post type to a repeater sub-field on a separate custom post type. The repeater rows are attendance records that contain the person’s name, arrival, departure, etc. In one CPT just a single arrival and departure time is logged but in the other, multiple attendance entries can show up (for the same person) because some people leave and come back during the day.

    Syncing the arrival time is easy since it just requires updating the first attendance record (repeater row) in the other CPT. Syncing the departure time is not acting as expected with multiple attendance entries for the same person. Here’s an outline of how I’m attempting this:

    • I’m using the acf/pre_save_post filter and doing some checks to see what values have been updated in the original CPT repeater
    • Then, I’m doing a separate query in a function to count the total number of attendance records in the CPT where multiple can exist to get the total
    • I set up the normal WP_Query and if have_rows/while have_rows
    • Within the loop I’m checking the ID of the person (post object sub-field) from the one that was updated
    • If the person ID matches I’m then doing a check to make sure the departure attendance record is the last one with a counter variable (at this point I can use the get_field() function and it returns the value I’m wanting to update
    • After update_sub_field() fires and I check the results in the other CPT I’m seeing that the next repeater row’s departure time is being updated. This doesn’t make any sense to me because I’m checking against the person’s ID before making use of update_sub_field

    I’ve also tried using the array method for the selector to explicitly indicate which row should be updated but when there are multiple rows with the same person’s name I’m seeing the same thing as described above where the next attendance row (different person) is changed.

    Code I’m working on:

    /**
     * Update the indicated arrival/departure time in the attendance sheet.
     *
     * @param string $time_type "arrival" or "departure" indication.
     * @param string $new_value New value for the arrival or departure time.
     * @param integer|false $day_to_update Attendance day to update.
     * @param integer $participant_id Participant ID to update.
     */
    function update_attendance_time( $time_type, $new_value, $day_to_update, $participant_id ) {
    
    	// Find the day in the attendance posts.
    	$args = array(
    		'no_found_rows'          => true,
    		'post_type'              => 'attendancesheet',
    		'posts_per_page'         => 1,
    		'title'                  => $day_to_update,
    		'update_post_term_cache' => false,
    	);
    
    	$attendance_query = new WP_Query( $args );
    	$current_entry_count = 1;
    	$participant_entry_count = 0;
    
    	// Count the number of repeater attendance entries for our participant to find last departure row.
    	if ( 'departure' === $time_type ) {
    		$participant_entry_count = get_attendance_entries_count( $args, $participant_id );
    	}
    
    	while ( $attendance_query->have_posts() ) {
    		$attendance_query->the_post();
    		$post_id = get_the_ID();
    
    		// Look for the attendance entries for our participant.
    		if ( have_rows( 'attendance_entries' ) ) {
    
    			// Standard loop to update the sub-field.
    			while ( have_rows( 'attendance_entries' ) ) {
    				the_row();
    
    				// Participant post object.
    				$participant = get_sub_field( 'participant_name' );
    
    				if ( ! is_object( $participant ) ) {
    					continue;
    				}
    
    				if ( $participant->ID === $participant_id ) {
    
    					// If it's a departure update the last attendance record, otherwise update the first for arrival.
    					if ( ( 'departure' === $time_type ) && ( $current_entry_count === $participant_entry_count ) ) {
    						update_sub_field( $time_type, $new_value, $post_id );
    					} elseif ( 'arrival' === $time_type ) {
    						update_sub_field( $time_type, $new_value, $post_id );
    						break;
    					}
    
    					$current_entry_count++;
    				}
    			}
    		}
    	}
    
    	wp_reset_postdata();
    }
Viewing 1 post (of 1 total)

You must be logged in to reply to this topic.