我正在使用自定义查询按日期范围筛选帖子,我阅读了文档并搜索了许多示例,但这段代码对我来说似乎是正确的,我不知道为什么它不起作用!请帮帮我,这让我很粗鲁:(
function my_pre_get_posts( $query ) {
// do not modify queries in the admin
if( is_admin() ) {
return $query;
}
// only modify queries for \'event\' post type
if( isset($query->query_vars[\'post_type\']) && $query->query_vars[\'post_type\'] == \'pubmed\' ) {
if( isset($_GET[\'date_deb\'] ) && isset($_GET[\'date_fin\'] ) ) {
$date_debut = strtotime("01-01-".$_GET[\'date_deb\']);
$date_fin = strtotime("31-12-".$_GET[\'date_fin\']);
$date_debut = date(\'Ymd\',$date_debut);
$date_fin = date(\'Ymd\',$date_fin);
//echo $date_debut->format("Y");
$query->set( \'meta_query\' , array(
array(
\'key\' => \'datecreated\',
\'value\' => array( $date_debut , $date_fin ),
\'compare\' => \'BETWEEN\',
\'type\' => \'NUMERIC\',
)
));
}
}
// return
return $query;
}
add_action(\'pre_get_posts\', \'my_pre_get_posts\');