所以我有一个非常适合事件的循环,只显示未来的帖子。问题是我很想将不再是未来帖子的帖子在循环中多保留一天。
例:因此,如果活动(或安排的帖子(是 3 日晚上 8 点。截至目前,它在晚上 8 点被删除(这是一个问题,因为它可能会持续 4 小时(。
我希望帖子多保留一天,或者我可以更改的时间。
这是我当前的代码:
<?php
$args = array( 'post_type' => 'event', 'posts_per_page' => 50, 'post_status' => 'future', 'order' => 'ASC' );
$loop = new WP_Query( $args );
if ( have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();?>
<div class="teaser-event <?php the_field('highlight') ?>">
<div class="event-meta gold">
<div class="event-date"><?php the_time('M d'); ?></div>
<div class="event-time"><?php the_time('g:i A'); ?></div>
</div>
<div class="event-title">
<a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title(); ?>">
<?php the_title(); ?>
</a>
</div>
</div>
<?php endwhile; else: ?>
<p><?php _e('Sorry, no posts matched your criteria.'); ?></p>
<?php endif; ?>
似乎WP_Query
的时间参数能够指定确定的时间跨度,但不能指定无限的时间跨度,例如从现在到未来的帖子。WordPress文档建议使用posts_where过滤器进行时间相关查询。因此,您可以将其放在主题的functions.php
中:
// Create a new filtering function that will add our where clause to the query
function filter_where($where = '') {
// posts from yesterday into the future
$where .= ' AND post_date >= "' . date('Y-m-d', strtotime('-1 day')) . '"';
return $where;
}
在上面的代码中,您可以执行以下操作:
$args = array('post_type' => 'event', 'posts_per_page' => 50, 'order' => 'ASC');
add_filter('posts_where', 'filter_where');
$loop = new WP_Query($args);
remove_filter('posts_where', 'filter_where');
if ( have_posts() ) : while ( $loop->have_posts() ) : $loop->the_post();
添加和删除过滤器并不能使其成为最优雅的解决方案,因此您可以通过在主题functions.php
中定义一个返回$loop
对象的自定义function get_recent_and_future_posts()
来清理它。
我看了一下:http://codex.wordpress.org/Class_Reference/WP_Query
在页面下方有一个名为"时间参数"的部分。
我认为与其在未来寻找post_status,不如寻找日期大于当前日期的帖子 - 1 天。