不要以编程方式在wordpress中显示较旧的帖子

时间:2012-04-12 07:05:52

标签: php wordpress-theming wordpress

我正在使用“未来就是现在!”插件,并希望显示所有未来安排的帖子。唯一的问题是,在进入循环之前,如何进行类似(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();
    ?>

1 个答案:

答案 0 :(得分:1)

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

第二次编辑:类似查询,但有一个过滤器:

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