tax query – How can I properly merge arrays into a ‘tax_query’ so I only get results of ALL filters instead of ANY

Question

I’m building a search and filter page. I’ve come to a problem. I have 9 filters. The user will select none, one, or any number of 9 of them at a time. I’m trying to avoid writing endless combinations of queries to satisfy each of those options.

I want to only show results of ALL of the filters together. This code works fine:

$taxquery= array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'version-filter',
        'field'    => 'slug',
        'terms'    => 'filter1',
        'operator' => 'IN',
    ),
    array(
        'taxonomy' => 'version-filter',
        'field'    => 'slug',
        'terms'    => 'filter2',
        'operator' => 'IN',
    ),
    array(
        'taxonomy' => 'version-filter',
        'field'    => 'slug',
        'terms'    => 'filter3',
        'operator' => 'IN',
    ),
);

What I’d really like is to do something like this. I gather the values from the [GET] paramaters earlier.

$mergedarrays= [];
if(isset($getfilter1result)){
$filter1= array(
        'taxonomy' => 'version-filter',
        'field'    => 'slug',
        'terms'    => 'filter1',
        'operator' => 'IN',
    );
array_merge($mergedarrays, $filter1);
}
if(isset($getfilter2result)){
$filter2= array(
        'taxonomy' => 'version-filter',
        'field'    => 'slug',
        'terms'    => 'filter2',
        'operator' => 'IN',
    );
array_merge($mergedarrays, $filter2);
}
if(isset($getfilter3result)){
$filter3= array(
        'taxonomy' => 'version-filter',
        'field'    => 'slug',
        'terms'    => 'filter3',
        'operator' => 'IN',
    );
array_merge($mergedarrays, $filter3);
}
$taxquery= array(
    'relation' => 'AND',
    echo $mergedarrays; //obviously this is not the right way to do this
);

I tried this way, but this resulted in ANY of the results being shown, rather than ALL of them.

$termsin= [];
if(isset($filter1)){
  $filter1= filtered1;
  $termsin=$array_push($termsin, $filter1);
}
if(isset($filter2)){
  $filter2= filtered2;
  $termsin=$array_push($termsin, $filter2);
}
if(isset($filter3)){
  $filter3= filtered3;
  $termsin=$array_push($termsin, $filter2);
}

$taxquery= array(
        'taxonomy' => 'version-filter',
        'field'    => 'slug',
        'terms'    => $termsin,
        'operator' => 'IN',
);

How can I get this to work correctly? Am I missing some basic function that could come in handy right around now? Seems to happen alot thanks to y’all showing me how to use the basic functions.

I use this in my $args array. Here’s the main problem.

'tax_query' => array(
            array(
                'relation' => 'AND',
                array(
                    'taxonomy' => 'version-filter',
                    'field'    => 'slug',
                    'terms'    => array('field1', 'field2'),
                    'operator' => 'IN',
                ),
            ),  
        ),

when I add this into my query, it shows results that match the terms field1 OR field2. I need them to match both, so I’m asking is there a way to alter my wordpress query to make this happen?

Or do I need to write a different kind of code in PHP?

0
RCOTWUnite 3 weeks 2022-11-10T10:37:14-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse