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?

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.

Create a custom gallery with Elementor

With this code we gonna add a custom gallery to an elementor page

Display the total weight in the cart and order page of woocommerce

This code will allow you to know the weight of the order in the cart and order pages of woocommerce.

Custom CSS ID link to Elementor Nested Tabs

This script will allow us to run an URL directly to an Elementor tab

Create a dropdown from a Custom Post Type list

This code will allow us to display a drop-down menu which lists all the CPTs present in our ACF

Filter number of orders by country

Set up a submenu that allows us to filter a date range and display the number of orders depending on the country.
Let's talk ?

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