Support

Account

Home Forums General Issues Sorting using ACF Date field Reply To: Sorting using ACF Date field

  • Thank you very much for your answer.
    Eventually I’m done with the code below:

    $now = date('Y-m-d H:i:s');
    
            $meta_query = array(
    
                'meta_query'   => array(
    
                    'relation' => 'OR',
    
                    array(
                        'event_start' => array(
                            'key'     => 'start_date',
                            'compare' => '<',
                            'value'   => $now,
                            'type'    => 'DATETIME'
                        ),
    
                        'event_end'   => array(
                            'key'     => 'end_date',
                            'compare' => '>',
                            'value'   => $now,
                            'type'    => 'DATETIME'
                        ),
                    ),
    
                    array(
                       'event_upcoming' => array(
                            'key'       => 'start_date',
                            'compare'   => '>=',
                            'value'     => $now,
                            'type'      => 'DATETIME'
                        )
                    ),
    
                    array(
    
                        'event_past'  => array(
                            'key'     => 'end_date',
                            'compare' => '<',
                            'value'   => $now,
                            'type'    => 'DATETIME',
                        ),
                    ),
    
                ),
    
            );
            $query->set('meta_query', $meta_query);
    
            $query->set(
                'orderby',
                array(
                    'event_start' => 'DESC',
                    'event_end' => 'DESC',
                    'event_upcoming' => 'ASC',
                    'event_past' => 'DESC',
                )
            );
    
    $query->set('order', 'DESC');

    And I was able to get that order:
    1. ONGOING EVENTS
    2. PAST EVENTS
    3. UPCOMMING EVENTS

    Is it not possible to use WP_Query for PAST EVENTS to be last?

    Are there any other ways?