I wish to hide past events based on the custom date field I set up.
<?php
the_post();
// Get 'events' posts
$events_posts = get_posts( array(
'post_type' => 'events',
'posts_per_page' => -4, // Unlimited posts
'orderby' => 'meta_value',
'meta_key' => 'event_date',
'order' => 'ASC'
) );
if ( $events_posts ):
?>
This code is currently showing my events in order, but I want to hide events older than today's date?
If you want to filter data by custom post field you have to use
meta_query
for thisHere is working example:
Hope this helps!
Related answer: https://stackoverflow.com/a/42325398/5019802
I think you are talking about looping through the $event_posts result while excluding those events whose date is 'less than today'. Which is quite an unoptimized and inappropriate way to achieve what you want. You should let MySQL retrieve events whose date is greater than NOW(). If the event_date field is a DATETIME type, then modify your query to get only events whose 'event_date' is greater than NOW().