WP_Query ONLY Future Posts
-
The following code will output events in an ascending manner, but it will also retrieve past events.
<?php $args = array( 'post_type' => 'event', 'posts_per_page' => -1, 'meta_key' => '_event_start_date', 'orderby' => '_event_start_date', 'order' => 'ASC', ); $posts = get_posts($args); $chunked = array_chunk($posts, 6, false); foreach ($chunked as $chunk) : get_template_part('template-parts/grids/events-grid', null, [ 'posts' => $chunk,]); endforeach; wp_reset_postdata(); ?>
I only want to show events that are in the scope ‘future.’ Unfortunately, it appears that you cannot pass the ‘scope’ in the $args array to the WP_Query (in my case get_posts())
With that being said, I tried to use ‘compare’ => ‘>=’ against a ‘value’ => $today
$today = date('Y-m-d'); (this is the format of the date in the database)
$args = array( 'post_type' => 'event', 'posts_per_page' => -1, 'meta_key' => '_event_start_date', 'orderby' => '_event_start_date', 'order' => 'ASC', // 'meta_value' => $today, (this format will not retrieve any posts) 'value' => $today, (this format will not filter events greater than or equal to today) 'compare' => '>=', );
Any help is much appreciated.
The goal is to query all future/upcoming events, to exclude past events.
Viewing 6 replies - 1 through 6 (of 6 total)
Viewing 6 replies - 1 through 6 (of 6 total)
- The topic ‘WP_Query ONLY Future Posts’ is closed to new replies.