Home › Forums › General Issues › 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( array( 'key' => 'event_date', 'compare' => '>=', 'value' => $today, 'type' => 'DATE', ), ), 'meta_key' => 'event_date', 'orderby' => 'event_date', 'order' => ASC, 'posts_per_page' => 4 ); query_posts($args);
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!
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!
I have found a thread which should help you with the above query:
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.
The topic ‘Trouble hiding past events – help w/ meta_query’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
Are you building WordPress sites with ACF and @BeaverBuilder, and wanted to use your ACF Blocks in both the block editor and Beaver Builder?— Advanced Custom Fields (@wp_acf) May 10, 2023
The BB team recently added support for using ACF Blocks in Beaver Builder. Check it out 👇https://t.co/UalEIa5aQi
© 2023 Advanced Custom Fields.