2012-02-16 90 views
2

所以我有一个循环完美的事件,只显示未来的职位。问题是,我希望将帖子不再放在循环中的未来帖子中多一天。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; ?> 

回答

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(); 

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

+0

哇!这看起来很棒,我很感谢快速帮助!这些代码有些超出了我的技能范围,所以我很好奇你是否解释了......和第二代码部分的第二部分。另外,我很好奇,在使用它之前是否需要编辑这些内容? – Reuben 2012-02-16 20:05:15

+1

我删除了'...',第二个代码段是你想用你的代码开始。我不完全确定它是否会起作用。把它放在那里试一下。 – 2012-02-16 20:29:34

+0

好吧,看起来接近!它只显示最后一天内的帖子..我需要这些和未来的帖子:) – Reuben 2012-02-17 02:15:42