2013-03-22 65 views
0

我使用这个查询,显示我的帖子:的WordPress:添加过滤器query_posts只显示帖子以“日期”设置为今天或未来

$today = getdate(); 
$year=$today["year"]; 
$month=$today["mon"]; 
$day=$today["mday"]; 

query_posts($query_string.'order=ASC' . '&post.status=future,publish'.'&year='.$year.'&monthnum='.$month); 
?> 

现在我想添加一个过滤器,只显示帖子已经在今天出版或将来出版。

例如:今天是3月22日。 PostA于3月1日发布,PostB已于今天发布,PostC将于3月24日发布(状态'future'已在我的查询中,所以仍将显示)。 我的新查询过滤器应该只显示PostB和PostC。

非常感谢!

:-)

回答

0

试试这个:

// Create a new filtering function that will add our where clause to the query 
    function filter_where($where = '') { 
$today = date('Y-m-d'); 
    // posts for today and future 
$where .= " AND post_date >= $today "; 
return $where; 
    } 

    add_filter('posts_where', 'filter_where'); 
    $query = new WP_Query($query_string); 
    remove_filter('posts_where', 'filter_where'); 
+0

这是否进入了functions.php的? – user2191254 2013-03-23 06:59:02

+0

mh不,它不起作用。但也许我做错了。我只是将它粘贴到我的functions.php – user2191254 2013-03-23 07:09:12

0

试试这个:

$archive_year = get_the_time('Y'); $archive_month = get_the_time('m'); $archive_day = get_the_time('d'); query_posts('year=' .$archive_year .'&monthnum=' .$archive_month .'&day=' .$archive_day); 
+0

请解释此代码如何解决此问题。只有代码的答案没有多少意义。 – rgettman 2013-12-17 22:35:42

相关问题