Home › Forums › Bug Reports › 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’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();
}
The topic ‘update_sub_field updating different row’ is closed to new replies.
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 Cookie Policy. If you continue to use this site, you consent to our use of cookies.