簡體   English   中英

不要以編程方式在WordPress中顯示較舊的帖子

[英]Don't show older post in wordpress programmatically

我正在使用“未來就在眼前!” 插件,並希望顯示所有將來的時間表。 唯一的問題是,在進入循環之前,如何進行查詢(WHERE date> = $ currentdate)?

    <?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();
    ?>
query_posts(array('post_status' => 'future'));

編輯:上面是適合您的循環的簡單答案,但是作為默認解決方案,最好使用新的$ 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

2nd編輯:類似的查詢,但帶有過濾器:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM