UPDATE December 23 2014
有一种更好的方法
date_query
的属性
WP_Query
类别:
$args = array(
\'post_type\' => \'post\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'post_format\',
\'field\' => \'slug\',
\'terms\' => array( \'post-format-image\' )
)
),
\'cat\' => \'-173\',
\'post_status\' => \'publish\',
\'date_query\' => array(
\'column\' => \'post_date\',
\'after\' => \'- 30 days\'
)
);
$query = new WP_Query( $args );
OLD ANSWER
使用
Time Parameters in WP_Query()引用法典中的示例:
Return posts from the last 30 days:
// This takes your current query, that will have the filtering part added to.
$query_string = array(
\'post_type\' => \'post\',
\'tax_query\' => array(
array(
\'taxonomy\' => \'post_format\',
\'field\' => \'slug\',
\'terms\' => array( \'post-format-image\' )
)
),
\'cat\' => \'-173\',
\'post_status\' => \'publish\'
);
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = \'\' ) {
// posts in the last 30 days
$where .= " AND post_date > \'" . date( \'Y-m-d\', strtotime( \'-30 days\' ) ) . "\'";
return $where;
}
add_filter( \'posts_where\', \'filter_where\' );
$query = new WP_Query( $query_string );
remove_filter( \'posts_where\', \'filter_where\' );
<小时>
Edit(回答OP的最新问题)。
Avoid using query_posts. 您可以使用上述技术来更改主查询(需要一些额外的conditionals - 是主页,是名为“foobar”等的页面):
function wpse52070_filter_where( $where = \'\' , $query ) {
if( $query->is_main_query() && is_page( \'foobar\' ) ){
// posts in the last 30 days
$where .= " AND post_date > \'" . date( \'Y-m-d\', strtotime( \'-30 days\' ) ) . "\'";
}
return $where;
}
add_filter( \'posts_where\', \'wpse52070_filter_where\' );