Filtering and ordering by ACF

[ad_1]

Hello, I have created something like Event manager with CPT + ACF. So I am using the “event_date” custom field (type datepicker), which could be simply different from publish_date (coming from WP core…).

I need to do two things now
1. Filter CPT, which are next, and which are past (based on $today variable – if greather than -> next, else past) – THIS IS DONE
2. Order posts by ACF – “event_date”, now my posts are ordered by publish date, which is not correct.

This is my code:

function lava_show_next_events($query) {
    //var_dump($query);
    $today = date('Ymd');
    
    $meta_query = $query->get( 'meta_query' );

    if ( ! $meta_query ) {
        $meta_query = [];
    }

    $meta_query[] = 
        array(
            'key' => 'datum_zaciatku',
            'value' => $today,
            'compare' => '>',
            'order_by' => 'datum_zaciatku',
            'order' => 'ASC'
        );

    //var_dump($meta_query);    

    $query->set( 'meta_query', $meta_query );
    //var_dump($query);
}

add_action( 'elementor/query/lava-next-events', 'lava_show_next_events',10 );

I am using Elementor with query ID, so I am trying to catch the query of CPT and update ordering. I was able to filter only future events, however ordering by ACF doesn’t work.

Note: datum_zaciatku means event_date 🙂

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer