0

I have a problem with making something like taxonomy filter. My code is similar to this:

$args = array(
'post_type' => 'post',
'tax_query' => array(
    'relation' => 'AND',
    array(
        'taxonomy' => 'movie_genre',
        'field'    => 'slug',
        'terms'    => array( 'action', 'comedy' ),
    ),
    array(
        'taxonomy' => 'actor',
        'field'    => 'id',
        'terms'    => array( 103, 115, 206 ),
        'operator' => 'NOT IN',
    ),
),
);
$query = new WP_Query( $args );

Okay. So basically I have a form with several select inputs. I have looped terms into every select input. When I select one and click Submit, it will send me to another page where are queryd products with this term (term is in product attributes). But when I select two values on the first page, the next page should show only these products what have both terms in it. That relation AND didn't work because if some selectbox was left empty the $GET value is empty and so the term is empty. My terms are made like that:

$car = $_GET["car"];

And then I put these variables into array like this:

    array(
        'taxonomy' => 'pa_my_taxonomy',
        'field'    => 'name',
        'terms'    => $car,
    ),

Address is like that:

http://mydomain/page/?car=auto&something=suvi&something2=&something3=&something4=&something5=&something6=

All my taxonomys are in that array. So if I have AND there, it will take into account every variable from address bar and if some of them is empty but product have something there it won't query that product.

For simple version I need to make car tires select form where I can select car type, season, tire diamenter and so on. If I select car type ordinary car and season winter then it would show me only tires with these product attributes.

  • question for you, are you trying to select multiple cars? Like ford, honda, chevrolet? And you would like to have all of these terms in an array? – Robbiegod Oct 16 '17 at 20:08
1

You would have to add the tax_query parameters depending on if the value is there in the GET parameters.

So it would be something like this,

$args = array(
           'post_type' => 'post',
           'tax_query' => array( 'relation' => 'AND', ) );
$car = $_GET["car"];
if(!empty($car)){
       array_push( 
                  $args['tax_query'],
                  array(
                          'taxonomy' => 'pa_my_taxonomy',
                          'field'    => 'name',
                          'terms'    => $car,
                       )
                 );
}
  • I tried it already. I didn't mention it because I hoped there will be some different ideas and that was just my testing. Maybe I did something wrong. – Hener Nov 10 '14 at 12:39
0

Still haven't find answer.

My version of the last comment code was like this:

$car = $_GET["car"];

$car_array = array('taxonomy' => 'pa_my-taxonomy','field' => 'name','terms' => $car);

$taxquery = array();

if (isset($car)) {
        array_push($taxquery, $car_array);
}

// WP_Query arguments
    $args = array (
        'post_type'              => 'product',
        'order'                  => 'ASC',
        'orderby'                => 'title',
        'tax_query'              => $taxquery,
    );

So basically it' is same thing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.