1

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

1 回答 1

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' );
于 2012-04-12T08:29:56.477 回答