我已经写了一个在过去一周中获取浏览量最大的帖子的短代码。我在shortcode函数中使用了如下过滤器:
function weeks_popular(){
//Filter the date
function filter_where($where = \'\') {
$where .= " AND post_date > \'" . date(\'Y-m-d\', strtotime(\'-7 days\')) . "\'";
return $where;
}
add_filter(\'posts_where\', \'filter_where\');
$pops = new WP_Query(
array(
\'posts_per_page\' => 4,
\'meta_key\' => \'views\',
\'orderby\' => \'meta_value_num\',
\'order\' => \'DESC\'
)
);
//If there is a post, start the loops
if ($pops->have_posts()) {
$pops_content=\'<div class="shortcode">\';
while ($pops->have_posts()){
$pops->the_post();
$pops_content .= \'<a href="\'.get_the_permalink().\'">\'.get_the_title().\'</a>\';
}
$pops_content .= \'</div>\';
return $pops_content;
}
//Remove the date filter
remove_filter(\'posts_where\', \'filter_where\');
wp_reset_postdata();
}
add_shortcode( \'recent-posts\', \'weeks_popular\' );
我以前在PHP小部件中使用过它,它工作得很好。现在,它有一个问题:它会影响页面上的所有其他查询,包括其他短代码。我在查询完成后重置post数据,所以我不知道出了什么问题,因为它过去在PHP小部件中工作(现在仍然工作)。只是在短代码中不起作用。
我错过什么了吗?