Wordpress:仅显示未来的帖子减去一天

时间:2012-02-16 18:50:30

标签: php wordpress

所以我有一个完美的活动循环,只显示未来的帖子。问题是,我希望在循环中保留不再是未来帖子的帖子一天。

实施例: 因此,如果活动(或预定的帖子)是在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; ?>

2 个答案:

答案 0 :(得分:3)

似乎WP_Query的时间参数能够指定明确的时间跨度,但不能指定不确定的时间跨度,例如从现在到未来的帖子。 WordPress文档建议使用posts_where filter进行时间相关查询。所以你可以把它放在你的主题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();

添加和删除过滤器不会使这成为最优雅的解决方案,因此您可以通过在主题function get_recent_and_future_posts()中定义返回任何对象functions.php的自定义$loop来清理它。是的。

答案 1 :(得分:1)

我看了一眼:http://codex.wordpress.org/Class_Reference/WP_Query

在页面下方有一个名为“时间参数”的部分。

我认为将来您不希望查找post_status,而是希望查找日期大于当前日期的帖子 - 1天。

相关问题