Home Forums General Issues Trouble hiding past events – help w/ meta_query


Trouble hiding past events – help w/ meta_query

  • I’ve used ACF to create a custom field “event_date” which is designed to display the date of upcoming events. The user simply has to choose “Events” as the category when adding a new post and they’ll see this field. That part works great. The trouble starts when I try to hide past events from displaying.

    Here’s my code:

    $today = time();
    $args = array(
    	'category_name' => 'events',
    	'meta_query' => array(
    			'key' => 'event_date',
    			'compare' => '>=',
    			'value' => $today,
    			'type' => 'DATE',
    	'meta_key' => 'event_date',
    	'orderby' => 'event_date', 
    	'order' => ASC,
    	'posts_per_page' => 4

    And then the loop, of course. The result of this code is that no posts are displayed, just my “There are no upcoming events” message. If I take out the part where I try to hide past posts (that whole meta_query section), I see my posts just fine – though past events do show, of course.

    Does anybody know what I’m missing here? How can I get past events to hide? Would love to get this working, thanks!

  • Hi @davidzumini,

    The meta query does not use the epoch time format. Have you tried using he WordPress date function? Eg:

    $today = current_time('mysql');
  • Thanks for the reply. I figured it may be that “$today” and “event_date” aren’t in the same time format, but I don’t know how to confirm or fix that. Unfortunately, your suggestion did not change the results.

    Is there something else that could be going on here? From the examples I’ve been looking at, this should be more straightforward than it has proved to be – and it has me stumped!

  • eliot – Thanks. It pointed me in the right direction. The real issue was that I was using a “Date & Time” field and the query wasn’t getting that. By changing my field type to just “Date” everything appears to be working.

    If you know of a way to get it to work with Date & Time, that might be nice, but if not this should suffice. Thank you.

Viewing 5 posts - 1 through 5 (of 5 total)

The topic ‘Trouble hiding past events – help w/ meta_query’ is closed to new replies.