CPT UI分类法多bullhorn_id筛选失效问题求助
多值bullhorn_id筛选失效问题
现有通过slug筛选职位的链接可正常工作,例如:
mydomain.com/filter/?technology=angular&category=software-development
需要实现多个bullhorn_id值的筛选,尝试了两种方式均无效:
- 逗号分隔参数:
mydomain.com/filter/?bullhorn_id=2979,2090
- 重复参数名:
https://itds.pl/filter/?bullhorn_id=2979&bullhorn_id=2903
单个bullhorn_id参数可正常筛选,多值时失效。
现有相关代码
生成筛选链接的部分代码:
if(isset($_GET['bullhorn_id'])){ $bullhorn = explode(',',$_GET['bullhorn_id']); if(count($bullhorn)>=0){ for($p=0;$p<count($bullhorn);$p++){ $filter[] = array( 'taxonomy' => 'bullhorn_id', 'field' => 'slug', 'terms' => $bullhorn[$p] ); } } else{ $filter[] = array( 'taxonomy' => 'bullhorn_id', 'field' => 'slug', 'terms' => $_GET['bullhorn_id'] ); } }
查询函数中的相关代码:
// Filter for search query if(isset($_REQUEST['input_keyword']) && $_REQUEST['input_keyword']!=''){ $arg['s'] = $_REQUEST['input_keyword']; } // Set meta query with argument if(isset($_REQUEST['location']) && $_REQUEST['location']!=''){ $filter[] = array ( 'taxonomy' => 'location', 'field' => 'slug', 'terms' => $_REQUEST['location'] ); } if(isset($_REQUEST['technology']) && $_REQUEST['technology']!=''){ $filter[] = array ( 'taxonomy' => 'technology', 'field' => 'slug', 'terms' => $_REQUEST['technology'] ); } if(isset($_REQUEST['product_category']) && $_REQUEST['product_category']!=''){ $filter[] = array ( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $_REQUEST['product_category'] ); } if(isset($_REQUEST['employment_type']) && $_REQUEST['employment_type']!=''){ $filter[] = array ( 'taxonomy' => 'employment_type', 'field' => 'slug', 'terms' => $_REQUEST['employment_type'] ); } if(isset($_REQUEST['seniority']) && $_REQUEST['seniority']!=''){ $filter[] = array ( 'taxonomy' => 'seniority', 'field' => 'slug', 'terms' => $_REQUEST['seniority'] ); } if(isset($_REQUEST['type_of_work']) && $_REQUEST['type_of_work']!=''){ $filter[] = array ( 'taxonomy' => 'type_of_work', 'field' => 'slug', 'terms' => $_REQUEST['type_of_work'] ); } if(isset($_REQUEST['language']) && $_REQUEST['language']!=''){ $filter[] = array ( 'taxonomy' => 'language', 'field' => 'slug', 'terms' => $_REQUEST['language'] ); } if(isset($_REQUEST['skills']) && $_REQUEST['skills']!=''){ $filter[] = array ( 'taxonomy' => 'skills', 'field' => 'slug', 'terms' => $_REQUEST['skills'] ); } if(isset($_REQUEST['bullhorn_id']) && $_REQUEST['bullhorn_id']!=''){ $filter[] = array ( 'taxonomy' => 'bullhorn_id', 'field' => 'slug', 'terms' => $_REQUEST['bullhorn_id'] ); } if(isset($_REQUEST['hot_offer']) && $_REQUEST['hot_offer']!=''){ $filter[] = array ( 'taxonomy' => 'hot_offer', 'field' => 'slug', 'terms' => $_REQUEST['hot_offer'] ); } if( !empty($filter) && count($filter)>1 ){ $arg['tax_query'] = $filter; } if(isset($_REQUEST['max_price']) && $_REQUEST['max_price']!=''){ $filtermeta[] = array( 'key' => 'maximum_price', 'value' => array(trim($_REQUEST['max_price']), 1000000), 'compare' => 'BETWEEN', 'type' => 'numeric' ); } if( !empty($filtermeta) && count($filtermeta)>1 ){ $arg['meta_query'] = $filtermeta; }
问题原因及解决方案
核心问题:
- 代码存在重复处理bullhorn_id的逻辑:生成筛选链接的代码尝试拆分逗号参数,但查询函数又单独从
$_REQUEST['bullhorn_id']取值添加筛选条件,导致逻辑冲突。 - 逗号分隔参数时,查询函数直接把整个字符串作为
terms值而非数组;重复参数名时,PHP仅保留最后一个参数值。 - 原代码仅当
count($filter)>1时才赋值tax_query,若仅筛选bullhorn_id多值,会因count($filter)=1导致筛选不生效。
修复步骤:
1. 删除重复的bullhorn_id处理逻辑
删掉查询函数中这段单独处理bullhorn_id的代码:
if(isset($_REQUEST['bullhorn_id']) && $_REQUEST['bullhorn_id']!=''){ $filter[] = array ( 'taxonomy' => 'bullhorn_id', 'field' => 'slug', 'terms' => $_REQUEST['bullhorn_id'] ); }
2. 优化bullhorn_id参数处理逻辑
替换生成筛选链接的代码,兼容逗号分隔和数组参数两种传递方式:
if(isset($_REQUEST['bullhorn_id'])){ // 处理数组格式参数(链接需写为bullhorn_id[]=xxx&bullhorn_id[]=xxx) if(is_array($_REQUEST['bullhorn_id'])){ $bullhorn_ids = $_REQUEST['bullhorn_id']; } else { // 处理逗号分隔参数,过滤空值 $bullhorn_ids = array_filter(explode(',', $_REQUEST['bullhorn_id'])); } if(!empty($bullhorn_ids)){ // 直接传递数组作为terms,无需循环添加多个筛选条件 $filter[] = array( 'taxonomy' => 'bullhorn_id', 'field' => 'slug', 'terms' => $bullhorn_ids, 'operator' => 'IN' // 显式指定IN操作符,默认值为IN,写清更直观 ); } }
3. 修复tax_query赋值逻辑
将原代码中count($filter)>1的判断改为只要$filter非空就赋值:
if(!empty($filter)){ $arg['tax_query'] = $filter; }
4. 对应调整前端请求链接
- 逗号分隔格式:
mydomain.com/filter/?bullhorn_id=2979,2090 - 数组参数格式(更规范):
mydomain.com/filter/?bullhorn_id[]=2979&bullhorn_id[]=2903
内容的提问来源于stack exchange,提问作者Tanya Nami
相关产品推荐
相关产品推荐

