Filter Elementor Post Widgets by date of an ACF field

For a customer requirement I had to display the posts via the Elementor Post widget in ascending date and time order. The time and date field is managed via a date and time picker in ACF, because posts are created via ACF.

Here is the code to add to your functions.php. The highlighted lines can be modified as desired.
You will also need to modify the server filtering field in the Elementor widget in order to filter with the function you have just created.

In my case, the ACF field is called date_and_time, I display it in a DESC manner and line 17 I generate the name of the query (which I will note in the elementor widget)

					function filter_posts_by_acf_date( $query ) {
    // Vérifiez que la meta_key 'date_et_heure' existe
    $meta_query = array(
            'key' => 'date_et_heure',
            'compare' => 'EXISTS',  

    $query->set( 'meta_key', 'date_et_heure' );
    $query->set( 'orderby', 'meta_value' );
    $query->set( 'order', 'DESC' ); // Pour un ordre décroissant. Utilisez 'ASC' pour un ordre croissant.
    $query->set( 'meta_query', $meta_query );

// Remplacez 'filtrage_date' par l'ID de votre requête personnalisée que vous avez défini dans l'widget Elementor
add_action( 'elementor/query/filtrage_date', 'filter_posts_by_acf_date' );

Want to leave a comment ?

Leave a Reply

Your email address will not be published. Required fields are marked *

Want to see more code snippets?
Let's talk ?

A question about WordPress?
A web project to be outsourced to a freelancer?
I am your man!