Thursday, November 10, 2022
HomeWordPress Developmenttax question - How can I correctly merge arrays right into a...

tax question – How can I correctly merge arrays right into a ‘tax_query’ so I solely get outcomes of ALL filters as a substitute of ANY


I am constructing a search and filter web page. I’ve come to an issue. I’ve 9 filters. The person will choose none, one, or any variety of 9 of them at a time. I am making an attempt to keep away from writing countless mixtures of queries to fulfill every of these choices.

I wish to solely present outcomes of ALL of the filters collectively. This code works fantastic:

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

What I would actually like is to do one thing like this. I collect the values from the [GET] paramaters earlier.

$mergedarrays= [];
if(isset($getfilter1result)){
$filter1= array(
        'taxonomy' => 'version-filter',
        'discipline'    => 'slug',
        'phrases'    => 'filter1',
        'operator' => 'IN',
    );
array_merge($mergedarrays, $filter1);
}
if(isset($getfilter2result)){
$filter2= array(
        'taxonomy' => 'version-filter',
        'discipline'    => 'slug',
        'phrases'    => 'filter2',
        'operator' => 'IN',
    );
array_merge($mergedarrays, $filter2);
}
if(isset($getfilter3result)){
$filter3= array(
        'taxonomy' => 'version-filter',
        'discipline'    => 'slug',
        'phrases'    => 'filter3',
        'operator' => 'IN',
    );
array_merge($mergedarrays, $filter3);
}
$taxquery= array(
    'relation' => 'AND',
    echo $mergedarrays; //clearly this isn't the suitable approach to do that
);

I attempted this manner, however this resulted in ANY of the outcomes being proven, moderately 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',
        'discipline'    => 'slug',
        'phrases'    => $termsin,
        'operator' => 'IN',
);

How can I get this to work accurately? Am I lacking some fundamental operate that would turn out to be useful proper round now? Appears to occur alot due to y’all displaying me easy methods to use the fundamental features.

I exploit this in my $args array. This is the principle drawback.

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

once I add this into my question, it exhibits outcomes that match the phrases field1 OR field2. I want them to match each, so I am asking is there a strategy to alter my wordpress question to make this occur?

Or do I want to jot down a distinct sort of code in PHP?

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments