2012-02-16 91 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