2012-04-12 55 views
1

我正在使用「未來現在!」插件,並希望顯示所有未來計劃的帖子。唯一的問題是,在我進入循環之前,我如何進行查詢(如WHERE date> = $ currentdate)?不要以編程方式在wordpress中顯示較早的帖子

<?php if (is_category('My awesome category')) { 
     $currentdate = date("Y-m-d",mktime(0,0,0,date("m"),date("d"),date("Y"))); 
     /* Some sort of query with the statement date >= $currentdate? */ 
    } 
    ?> 

    /* The Loop */ 
    <?php if (have_posts()) : while (have_posts()) : the_post(); 
    ?> 

回答

1
query_posts(array('post_status' => 'future')); 

編輯:以上是簡單的答案與你的循環適合,但作爲一個默認的解決方案,最好的是u使用新的$ WP_Query對象:

$my_query = new $WP_Query; 
$my_query->query_posts(array('post_status' => 'future')); 

while ($my_query->have_posts()) : 
    $my_query->the_post(); 
    the_title(); 
endwhile; 

wp_reset_postdata(); // now the main wordpress query and post data is intact 

2日編輯:類似查詢,但有一個過濾器:

function filter_where($where = '') { 
    // posts in the future 
    $now = date("Y-m-d H:i:s"); 
    $where .= " AND post_date >= '$now'"; 
    return $where; 
} 

add_filter('posts_where', 'filter_where'); 
$q = new WP_Query(array('post_type' => 'post')); 
while ($q->have_posts()) : 
    $q->the_post(); 
    the_title(); 
endwhile; 
remove_filter('posts_where', 'filter_where'); 
+0

與插件的問題「未來就是現在」它集所有未來後發佈。所以沒有post_status命名爲未來的帖子。那麼是否可以創建一個查詢來檢查post_date並與當前日期進行比較? – David 2012-04-12 21:37:38

+0

是的,你可以做到這一點。我做了一個編輯。 – offroff 2012-04-13 00:04:22