jef vankerckhoven
jef vankerckhoven

Reputation: 1

Wordpress wp_query - my date_query doesn't work for filtering upcoming events

This code doesn't return any posts. Is there something wrong with the date? I was thinking it might use the creation date and not the publishing date.

  <?php
  $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
  $args = array(
    'post_type' => 'post',
    'post_status' => 'publish',
    'category_name' => 'events',
    'orderby' => 'date',
    'order' => 'DESC',
    'date_query' => array(
      'column' => 'post_date',
      'after' => 'today',
      'inclusive' => true,
      ),
    'posts_per_page' => 3,
    'paged' => $paged,
  );
  $arr_posts = new WP_Query($args);

  if ($arr_posts->have_posts()) :
    while($arr_posts->have_posts()) :
      $arr_posts->the_post();
      ?>
      <div class='event <?php $tags = get_the_tags();
          if ($tags) {
              foreach( $tags as $tag ) {
                  echo $tag->name;
              }
          }?>'
        id="post-<?php the_ID(); ?>" >
        <h1 class="blue-title"><?php the_title();?></h1>
        <div style="display: block;">
          <p style="width: 50%; float: left;"><?php the_date(); ?></p>
          <p style="width: 50%; float: left; text-align: right;"><?php $tags = get_the_tags(); if ($tags) {foreach( $tags as $tag ) {echo $tag->name . " ";} }?> </p>
        </div>
        <p><?php the_excerpt(); ?></p>

        <a class="right post_link" href="<?php the_permalink(); ?>">Read more</a>
        <div style="clear: both;"></div>
      </div>
      <?php
    endwhile;
    ?>
    <?php if (function_exists("pagination")) {
        pagination($arr_posts->max_num_pages);
    } ?>
  <?php else : ?>
    <h1>Sorry, there are no events available yes.</h1>
  <?php
  endif;
  ?>

I tried using another date at the 'after' query. It works if I use first of june for example.

Upvotes: 0

Views: 1817

Answers (2)

Khorram Khondkar
Khorram Khondkar

Reputation: 184

I was having the same problem when comparing between dates.

after googling a lot I found the commit in core wordpress and it worked for me: as per the changes in here inclusive commit wordpress

'after' => array( 
    'year' => 2020,
    'month' => 10,
    'day'  => 01
),
'inclusive' => true

you need to set the array exactly as the commit in that link.

Upvotes: 0

Elvin Haci
Elvin Haci

Reputation: 3572

Just follow WordPress Codex and such problems would be solved.

Date Query parameter accepts arguments as an array. Not directly. So in your case

'date_query' => array(
        array(
            'year'  => 2012,
            'month' => 12,
            'day'   => 12,
        ),
    ),

You need to use array inside array.

    'date_query' => array(
         array(
          'column' => 'post_date',
          'after' => 'today',
          'inclusive' => true,
         )
),

Upvotes: 2

Related Questions