Skip to content

Instantly share code, notes, and snippets.

@askwpgirl
Created February 15, 2021 20:00
Show Gist options
  • Save askwpgirl/5caa4f2b090a117b9d3285ecb724ae05 to your computer and use it in GitHub Desktop.
Save askwpgirl/5caa4f2b090a117b9d3285ecb724ae05 to your computer and use it in GitHub Desktop.
Show Elementor Posts after today's date for events using ACF date field
<?php
//Info about custom query filters:
//https://developers.elementor.com/custom-query-filter/ combined with
// https://www.advancedcustomfields.com/resources/date-time-picker/
// The query ID goes in the Elementor Query under the Query area of the post list widget (see screenshot).
// The function below goes in your child theme's functions.php file or use the Code Snippets plugin to add it.
/*-------------------------------------------------------*/
/* Show Event posts in date order and remove past dates
/*-------------------------------------------------------*/
add_action( 'elementor/query/event_filter', function( $query ) {
$today = date('Y-m-d');
$query->set( 'meta_query', array(
array(
'key' => 'event_date',
'compare' => '>',
'value' => $today,
'type' => 'DATE',
)
) );
$query->set('orderby', 'meta_value');
$query->set('order', 'ASC');
$query->set('meta_key', 'event_date');
} );
@kpfdigital
Copy link

Thanks Angela. I made a couple of changes on my use;

  1. I changed $today = date('Y-m-d'); to $today = date_i18n ('Y-m-d'); so that today’s date ends at midnight local time for my site.

  2. I changed the compare to "greater than or equal to" so that an event stays in the upcoming dates list until the end of the day that it occurs in.

@jzaleskidesign
Copy link

You literally saved my life with this thank you so much!

@manuel-will
Copy link

Been busting my head creating a query like that. Love it, thank you so much. I was able to easily adapt it to show only events that have passed.

@kennymcnett
Copy link

@kpfdigital Thanks for your notes!
@askwpgirl Thanks for your initial code!

I also changed the > to >= and it worked how I wanted it to.
From: 'compare' => '>',
To: 'compare' => '>=',

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment