Home > OS >  Filter with two id of the same value CPT UI(taxanomies)
Filter with two id of the same value CPT UI(taxanomies)

Time:08-26

I have a special link with a filter, so I filter things with slugs through the link, for example

mydomain.com/filter/?technology=angular&category=software-development

and it shows the chosen jobs

Now, I want to show jobs with different id's but with the same slug, like here:

mydomain.com/filter/?bullhorn_id=2979,2090

I tried this variation

https://itds.pl/filter/?bullhorn_id=2979&bullhorn_id=2903

and it's not working With one ID it works perfectly Could please someone help me? I'm not good in PHP here is part of a code that generates link

 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']  );
                        }
                    }

Oh and BTW it's taxonomy, and the costume template for the page The QUERY is in the function and looks like this:

     // 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;   
    }



 

CodePudding user response:

Here's something that could work for you:

Just replace following:

if(isset($_REQUEST['bullhorn_id']) && $_REQUEST['bullhorn_id']!=''){
        $filter[] = array (
                'taxonomy' => 'bullhorn_id',
                'field' => 'slug',
                'terms' => $_REQUEST['bullhorn_id']
            );
    }

With following in your 2nd code:

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.
        );
    }  
}

let me know if this worked for you or if we need to tweak something in it to make it work. I believe it'll work.

CodePudding user response:

The issue was a bit stupid, the problem was that the previous developer set the logic to the 'AND' and I needed it to be 'OR', so we just changed this simple part and it started working, without any JSON encoding or something

here

<?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']  ) ;
                        }
                    }
  • Related