在哪里
The
WP_Comment_Query
类的子句具有以下筛选器:
$pieces = array( \'fields\', \'join\', \'where\', \'orderby\', \'order\', \'limits\', \'groupby\' );
$clauses = apply_filters_ref_array( \'comments_clauses\', array( compact( $pieces ), &$this ) );
那意味着你可以
comments_clauses
并过滤
$clauses[\'where\']
部分
怎么做
自WP 3.7以来,我们得到了
WP_Date_Query
班其工作原理如下(简化示例):
期间使用的默认值WP_Query
- 通过查询变量检索:
array(
\'year\' => \'\',
\'monthnum\' => \'\',
\'week\' => \'\',
\'day\' => \'\',
\'hour\' => \'\',
\'minute\' => \'\',
\'second\' => \'\',
)
如何进行
date_query
:
复制自的代码示例here$dateQuery = new WP_Query( array(
\'date_query\' => array(
\'column\' => \'optional, column to query against, default is post_date\',
\'compare\' => \'optional, see WP_Date_Query::get_compare()\',
\'relation\' => \'optional, OR or AND, how the sub-arrays should be compared, default is AND\',
array(
\'column\' => \'see above\',
\'compare\' => \'see above\',
\'after\' => \'string or array, see WP_Date_Query::build_mysql_datetime()\',
\'before\' => \'string or array, see WP_Date_Query::build_mysql_datetime()\',
\'inclusive\' => \'boolean, for after/before, whether exact value should be matched or not\',
\'year\' => \'4 digit int\',
\'month\' => \'int, 1-12\',
\'week\' => \'int, 0-53\',
\'day\' => \'int, 1-31\',
\'hour\' => \'int, 0-23\',
\'minute\' => \'int, 0-60\',
\'second\' => \'int, 0-60\',
),
),
// ... other query args
) );
你也可以做一个简单的
WP_Date_Query()
并获取生成的SQL字符串,以便稍后在子句筛选器中使用:
$dateQuery = WP_Date_Query( array( /* your query */ ) );
$whereDate = $dateQuery->get_sql();
// Inspect result
var_dump( $whereDate );
现在,相同来源的帖子及其评论的日期查询:
// All comments that are within the past week
$some_comments = get_comments( array(
\'post_ID\' => get_the_ID(), // Can be omited as well
\'date_query\' => array(
array(
\'after\' => \'1 week ago\',
),
),
\'count\' => true // return only the comment count
) );
模型未测试。。。
global $wp_query;
$daysQuery = new WP_Query( array(
\'post_type\' => array( \'post\' ),
\'showposts\' => 6,
\'cat\' => \'mycat\',
\'ignore_sticky_posts\' => true,
\'orderby\' => \'comment_count date\',
\'date_query\' => array(
array(
\'after\' => array(
\'year\' => date( "Y" ),
\'month\' => date( "m" ),
\'day\' => "01",
),
\'before\' => array(
\'year\' => date( "Y" ),
\'month\' => date( "m", strtotime( "-1 Months" ) ),
\'day\' => date( "t", strtotime( "-1 Months" ) ),
),
// \'inclusive\' => true,
\'month\' => date( "m" ),
),
),
) );