Support

Account

Home Forums ACF PRO Sort a repeater field by date (and store it) Reply To: Sort a repeater field by date (and store it)

  • After some testing, I think this will do the trick.

    if ( isset( $_POST[ 'acf' ][ 'field_5a0de683a21cf' ] ) ) {
        $tour_dates = get_field( 'sd_tour_schedule', $post_id );
    
        if ( $tour_dates ) {
            foreach ( $tour_dates as $key => $row ) {
                $column_id[ $key ] = $row[ 'from' ];
            }
            array_multisort( $column_id, SORT_NUMERIC, $tour_dates );
            $key = 0;
            foreach ( $tour_dates as $key => $tour_info ) {
                if ( is_array( $tour_info ) && count( $tour_info ) > 0 ) {
                    foreach( $tour_info as $column => $value ) {
                        if ( 'city' != $column ) {
                            $value = date( 'Ymd', (int) $value );
                        }
                        update_post_meta( $post_id, 'sd_tour_schedule_' . $key . '_' . $column, $value );
                    }
                }
                $key++;
            }
        }
    }