使用WP_QUERY从今天开始随机获取帖子

时间:2014-12-18 作者:Locke

在data\\u query()的逻辑中;我找到了此WP\\U查询示例:

<!-- Second Senses Featured -->
    <ul id="featuredSecond" class="featured-second">

      <?php
      $today = getdate();
      $args = array(
        \'tag_slug__in\' => array( \'destacado\'),
        \'posts_per_page\' => 2,
        \'post_type\' => \'any\',
        \'offset\' => 3,
        \'orderby\' => \'rand\',
        \'date_query\' => array(
          array(
            \'after\' => \'Today\',
            \'inclusive\' => true,

            )
          ),
        \'post_status\' => array(
          \'publish\'
          )
        );

      $featured = new WP_Query($args);

      if ( $featured->have_posts() ) :
        while ( $featured->have_posts() ) : $featured->the_post();
      ?>

      <li id="<?php the_slug(); ?>-<?php the_id(); ?>">
        <article id="post-<?php the_id(); ?>" class="cf"  role="article">

          <?php featured_content(\'thumb-480x300\'); ?>

          <div class="featured-box">
            <h1 class="featured-title cf">
              <a href="<?php the_permalink() ?>">
                <?php the_title(); ?>
              </a>
            </h1>
          </div>
          <!--featured-box-->

        </article>

      </li>
      <?php
      endwhile;
      endif;
      wp_reset_postdata();
      ?>
但一切都在与3周大的帖子轮换。。或更多!

这种逻辑是如何不起作用的?

如果您注意到今天有一个名为$的变量,但我没有让它工作。。。

谢谢

3 个回复
最合适的回答,由SO网友:Robert hue 整理而成

我认为没有参数值Today 正如您在中使用的date_query.

如果你想返回今天的帖子,那么你应该向date_query 将当前日期存储在$today 大堆下面是您的查询。

      $today = getdate();
      $args = array(
        \'tag_slug__in\' => array( \'destacado\'),
        \'posts_per_page\' => 2,
        \'post_type\' => \'any\',
        \'offset\' => 3,
        \'orderby\' => \'rand\',
        \'date_query\' => array(
            array(
              \'year\'  => $today[\'year\'],
              \'month\' => $today[\'mon\'],
              \'day\'   => $today[\'mday\'],
            ),
          ),
        \'post_status\' => \'publish\',
        );

        $featured = new WP_Query($args);

SO网友:CarambaMoreno

您应该使用$today而不是“today”,如下所示:

$today = getdate();
$args = array(
    \'tag_slug__in\' => array( \'destacado\'),
    \'posts_per_page\' => 2,
    \'post_type\' => \'any\',
    \'offset\' => 3,
    \'orderby\' => \'rand\',
    \'date_query\' => array(
      array(
        \'after\' => $today,
        \'inclusive\' => true,
        )
      ),
    \'post_status\' => array(
      \'publish\'
      )
    );
  $featured = new WP_Query($args);

SO网友:mrbobbybryant

尝试替换getdate() 具有current_time(\'Y-m-d\')

结束

相关推荐

使用新的WP-Query()从循环中过滤后期格式;

嗨,我目前正在为我的博客构建一个主题。下面的代码指向最新的帖子(特色帖子)。因为这将有一个不同的风格比所有其他职位。然而我想过滤掉帖子格式:链接使用我在循环中定义的WP查询,因为它给我带来了更多的灵活性。我该怎么做呢? <?php $featured = new WP_Query(); $featured->query(\'showposts=1\'); ?> <?php while ($featured->have_post