When you use the Posts widget from Elementor, it allows you to order your posts by title, date, menu order, or random. However, what if you wanted to sort them based on a custom field date, used by Advanced Custom Fields or ACF?
You now can by doing the following:
- Be sure to have a custom date field created. In our example, we use, “event_departure_date”.
- Go to the page/template in Elementor that you want your posts widget on. Give a name in the “Query ID” field. In our example, we use, “event_sort_date”.
- Lastly, go to your theme’s functions.php file and add the following code:
/* Sort Posts by Custom Date Field */ add_action( 'elementor/query/event_sort_date', function( $query ) { $query->set( 'meta_key', 'event_departure_date' ); $query->set( 'orderby', 'meta_value_num' ); $query->set( 'order', 'ASC' ); });
Be sure to change “event_departure_date” and “event_sort_date” to your own.