/*
利用WordPress的 pre get posts 实现在归档页的多重筛选
需求举例:在分类归档页面添加标签筛选
支持自定义taxonomy
在分类页的网址基础上,添加GET参数:?tag_id= 或者 ?language_id=
叠加筛选 ?tag_id=xxx&language_id=xxx
*/
function my_pre_get_posts($query) {
// 排除后台管理界面
if (is_admin()) {
return $query;
}
if (is_archive() or is_search()) {
// taxonomy Begin 多重筛选开始 @2018.12.12
$tax_query = array(
'relation' => 'AND',
);
//1、标签
if(isset($_GET['tag_id'])){
$array_temp = array(
'taxonomy' => 'post_tag',
'field' => 'term_id',
'terms' => $_GET['tag_id'],
);
array_push($tax_query, $array_temp);
}
//2、自定义taxonomy 语言
if(isset($_GET['language_id'])){
$array_temp = array(
'taxonomy' => 'language',
'field' => 'term_id',
'terms' => $_GET['language_id'],
);
array_push($tax_query, $array_temp);
}
$query->set('tax_query',$tax_query);
//taxonomy End
}
return $query;
}
add_action('pre_get_posts', 'my_pre_get_posts');
文章源自www.xlme.cn川渝圈-https://www.xlme.cn/3910.html
