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)

  • This is my meta query.

    $query->set( 'meta_query', array(
        'tour_clause' => array(
            'key'     => 'sd_tour_schedule_%_until', 
            'value'   => date( 'Ymd' ),
            'compare' => '>'
        ),
        'move_up_clause' => array(
            'key'     => 'sd_move_item_up_timestamp',
            'compare' => 'EXISTS'
        ),
    ));
    $query->set( 'orderby', array( 'tour_clause' => 'ASC', 'move_up_clause' => 'DESC' ) );

    And the filter which is called.

    function sd_tour_city_filter( $where ) {
        global $wpdb;
        $where = str_replace(
            "meta_key = 'sd_tour_schedule_%",
            "meta_key LIKE 'sd_tour_schedule_%",
            $wpdb->remove_placeholder_escape( $where )
        );
        return $where;
    }

    It returns the output but sorts by the row numbers/index. Hence why the order needs to be correct in the back-end and that’s why the order has to be ‘re-stored’ after saving a post. I disabled the ordering of repeaters on the front-end so users can’t change this. I don’t want them to think about this.