查询Wordpress中特定日期后发布的帖子
我试图编写一个查询Wordpress中特定日期后发布的帖子,wordpress,Wordpress,我试图编写一个WP\u查询,我只调用2012年3月之后发布的帖子。我可以成功地给2012年3月刚刚发布的帖子打电话,但“从2012年3月起”我一直在努力 $current_year = date('2012'); $current_month = date('>3'); // This doesn't work $current_month = date('3'); // This DOES work $custom_query = new WP_Quer
WP\u查询
,我只调用2012年3月之后发布的帖子。我可以成功地给2012年3月刚刚发布的帖子打电话,但“从2012年3月起”我一直在努力
$current_year = date('2012');
$current_month = date('>3'); // This doesn't work
$current_month = date('3'); // This DOES work
$custom_query = new WP_Query("year=$current_year&monthnum=$current_month&order=ASC&posts_per_page=-1");
我是错过了一些简单的东西,还是这必须变得更复杂 中的“时间参数”部分有一个关于日期范围的注释。使用相同的技术:
$query_string = "order=ASC&posts_per_page=-1";
// Create a new filtering function that will add our where clause to the query
function filter_where( $where = '' ) {
$where .= " AND post_date >= '2012-03-01'";
return $where;
}
add_filter( 'posts_where', 'filter_where' );
$custom_query = new WP_Query( $query_string );
remove_filter( 'posts_where', 'filter_where' );
自WordPress版本3.7以来,有一个WP_Query参数非常适合这种类型的查询 ,您可以使用参数后的
指定日期查询。
之后的可以是与strotime()兼容的字符串,也可以是“年”、“月”、“日”值的数组
对于您的示例,类似于以下内容的内容应该可以工作:
$args = array(
'posts_per_page' => -1,
'date_query' => array(
'after' => array(
'year' => 2012,
'month' => 3,
'day' => 1,
),
),
);
$custom_query = new WP_Query( $args );
或使用strotime()-字符串:
您还可以通过添加'inclusive'=>true来将after日期包括在查询中,否则只包括第二个帖子日期查询,如alisspers使用strotime()所说的函数:strotime('-3天')
或strotime('-2周')
或strotime>(“下周四”)
阅读PHP文档中的更多选项:
$args = array(
'posts_per_page' => -1,
'date_query' => array( 'after' => '2012-03-01' ),
);
$custom_query = new WP_Query( $args );