WP_Query ONLY Future Posts | WordPress.org

[ad_1]

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.

  • This topic was modified 47 minutes ago by rftbdev.

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer