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(
        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?

Stock Status Widget for Elementor

Elementor widget displaying the stock status of WooCommerce products. Designed for the Loop Builder (product cards in archives/categories).

Back to Top for Elementor

“Back to Top” button with progressive fill based on scroll. Automatically uses Elementor's main color.

WooCommerce Gallery Hover for Elementor Loop

Displays the first image in the gallery when hovering over the featured image in Elementor loops.

Free shipping method with WooCommerce coupon

Offer free shipping… but only for the shipping method you choose.

WooCommerce Product Buyers List

Added a tab in WooCommerce to display buyers of a specific product with date filter.

Additional Emails per Products for WooCommerce

Adds a custom tab in WooCommerce products to write an email specific to each product. This will be sent in a dedicated email.
Let's talk ?

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