WordPress wp_query-我的date_query无法过滤即将发生的事件

时间:2019-06-13 09:50:07

标签: php wordpress

此代码不返回任何帖子。日期有问题吗?我以为它可能使用创建日期而不是发布日期。

  <?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;
  ?>

我尝试在“之后”查询中使用另一个日期。例如,如果我使用六月初,它就可以工作。

2 个答案:

答案 0 :(得分:1)

只需遵循WordPress Codex即可解决此类问题。

日期查询参数接受参数作为数组。不直接。因此,就您而言

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

您需要在array内部使用array。

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

答案 1 :(得分:0)

在日期之间进行比较时,我遇到了同样的问题。

经过大量的搜索之后,我发现核心wordpress中的提交对我有用: 根据这里的变化 inclusive commit wordpress

void PrintMessage1(char message[80], int value)
{
 sprintf(str,message,value);
  Serial.print(str);
  Serial.write(13); //carriage return character (ASCII 13, or '\r')
  Serial.write(10); //newline character (ASCII 10, or '\n')
}

您需要将数组设置为与该链接中的提交完全相同。