Introduction
In this tutorial, you’ll learn how to order your Elementor loop grid based on an ACF meta field. This is quite useful when showcasing upcoming events, ensuring that only current events are displayed in ascending order, starting with those closest to the current date.
Steps
Create and apply loop grid template
Begin by designing your loop grid template within your Elementor page, and apply the loop grid widget to the page. The loop grid widget is a very powerful and easy to use widget but Elementor Pro is required to use this feature.

Apply the custom query filter
By default, Elementor does not have an option to order by meta field. But fortunately for us, elementor has provided a way to apply a custom query filter. You can learn more about it from the Elementor Developer Docs.
Using your favourite method for applying custom PHP code snippets, such as using a script organiser plugin like Code Snippets Pro or WPCodeBox, or using your child theme (my preferred method), apply the code snippet provided in the next section.
Add query ID to loop grid
Finally, return back to your loop grid widget and apply the query ID “dd_event_date” under Content > Query > Query ID

PHP Code Snippet
Order by Date
Replace the start_date with your own ACF meta field key.
<?php
function dd_query_by_event_date( $query ) {
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_key', 'start_date' );
}
add_action( 'elementor/query/dd_event_date', 'dd_query_by_event_date' );
Limit the earliest date to today’s date
Replace the start_date with your own ACF meta field key.
<?php
function dd_query_by_event_date( $query ) {
$query->set( 'orderby', 'meta_value' );
$query->set( 'meta_key', 'start_date' );
$query->set( 'meta_query', array(
array(
'key' => 'start_date',
'value' => date( 'Y-m-d' ),
'compare' => '>=',
'type' => 'DATETIME',
),
) );
}
add_action( 'elementor/query/dd_event_date', 'dd_query_by_event_date' );



Leave a Reply