首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >具有两个id的相同值CPT (分类法)的过滤器

具有两个id的相同值CPT (分类法)的过滤器
EN

Stack Overflow用户
提问于 2022-08-24 09:04:10
回答 2查看 44关注 0票数 0

我有一个带有过滤器的特殊链接,所以我通过链接过滤一些东西,例如

代码语言:javascript
复制
mydomain.com/filter/?technology=angular&category=software-development

它显示了所选择的工作

现在,我想展示不同身份的工作,但使用相同的鼻涕虫,就像下面这样:

代码语言:javascript
复制
mydomain.com/filter/?bullhorn_id=2979,2090

我试过这种变体

代码语言:javascript
复制
https://itds.pl/filter/?bullhorn_id=2979&bullhorn_id=2903

而且它不是用一个ID工作的,它能很好地工作,能让人帮帮我吗?我不擅长PHP,这里是生成链接的代码的一部分

代码语言:javascript
复制
 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']  );
                        }
                    }

哦和BTW它的分类,和自定义模板的网页。

查询位于函数中,如下所示:

代码语言:javascript
复制
     // 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;   
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-08-25 11:57:27

这个问题有点愚蠢,问题是以前的开发人员将逻辑设置为' and‘,我需要它是' or ',所以我们只更改了这个简单的部分,它就开始工作了,没有任何JSON编码或其他东西。

这里

代码语言:javascript
复制
<?php
                    $paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
                    $filter = array('relation' => 'OR');//it was AND
                    $arg = array(
                      'post_type' => 'product'
                    );
                    // For get variable
                    if(isset($_GET['category'])){
                        $cat = explode(',',$_GET['category']);                            
                        if(count($cat)>=0){                                
                            for($i=0;$i<count($cat);$i++){                                 
                                $filter[] = array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $cat[$i]  ) ;
                            }
                        }
                        else
                        {
                            $filter[] = array( 'taxonomy' => 'product_cat', 'field' => 'slug', 'terms' => $_GET['category']  ) ;
                        }
                    }
票数 0
EN

Stack Overflow用户

发布于 2022-08-24 09:23:38

以下是一些对你有用的东西:

只需替换以下内容:

代码语言:javascript
复制
if(isset($_REQUEST['bullhorn_id']) && $_REQUEST['bullhorn_id']!=''){
        $filter[] = array (
                'taxonomy' => 'bullhorn_id',
                'field' => 'slug',
                'terms' => $_REQUEST['bullhorn_id']
            );
    }

在您的第二个代码中使用以下内容:

代码语言:javascript
复制
if(isset($_GET['bullhorn_id']) && $_REQUEST['bullhorn_id']!=''){
    //check if the value contains a comma if it does then explode and use them as an array to filter.
    if( strpos($_GET['bullhorn_id'], ',') !== false ) {
        $bullhorn = explode(',',$_GET['bullhorn_id']);
        $filter[] = array( 
            'taxonomy' => 'bullhorn_id', 
            'field' => 'term_id', //we need to find in IDs
            'terms' => $bullhorn //we have all ids in this array 
        );
    } else {
        $filter[] = array( 
            'taxonomy' => 'bullhorn_id', 
            'field' => 'slug', 
            'terms' => $_GET['bullhorn_id']  //we have slug here probably as you said, if it's ID then replace the field => slug with field => term_id like above code.
        );
    }  
}

让我知道这是否对你有用,或者如果我们需要调整它的东西,使它发挥作用。我相信会成功的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73470356

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档