1

I need wp_query with more than one taxonomies and terms which are dynamic. Multiple terms to one taxonomies each. Like If one taxonomy is Color it has terms blue,green,red Second taxonomy is Size and terms 7,8,9 this taxonomy is Price and its terms are 500$,600$ and so on.

I need to filter results based on checkboxes like happens on any e-commerce websites.Let's say first I select blue color then products are those which belong to blue then if I select green then products are those which belong to both blue and green but now if I select 7 then product will be those which belong to blue,green having size 7 .

Hope you are getting what I need.I saw on codex for multiple taxonomy but it is having only 2 taxonomies which are static but in my case it is dynamic and can be more than 5.

In codex it is the code..

$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 );$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 );

but what if i have more than 2 e.g 5(dynamic).

Please give me any solution.

I also write query intead of Wp-Query but not working it is adding result of blue,green with size 7 instead of filtering.

Here is query.

$taxonomy_name = "'" . implode("','", $taxonomy_name_ar) . "'";
$term_name = "'" . implode("','", $term_name_ar) . "'";

$querystr = "
    SELECT * 
    FROM $wpdb->posts
    LEFT JOIN $wpdb->term_relationships ON($wpdb->posts.ID = $wpdb->term_relationships.object_id)
    LEFT JOIN $wpdb->term_taxonomy ON($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id)
    LEFT JOIN $wpdb->terms ON($wpdb->term_taxonomy.term_id = $wpdb->terms.term_id)
    WHERE $wpdb->posts.post_type = 'product' 
    AND $wpdb->posts.post_status = 'publish'
    AND $wpdb->term_taxonomy.taxonomy in (".$taxonomy_name.")
    AND $wpdb->terms.slug in (".$term_name.")
    ORDER BY $wpdb->posts.post_date DESC
    LIMIT 10
    ";
echo $querystr;
$pageposts = $wpdb->get_results($querystr, OBJECT); 

Note $tanomy_name is having the number of taxonomies like color,size,price and terms having number of terms like 7,8,blue,green

UPDATE : AFTER ANSWER

$tax_query = array( 'relation' => 'AND' );


foreach($taxonomy_array as $taxonomy_array_value){

$term_value_array = array();

foreach($term_array[$taxonomy_array_value] as $term_value){

        array_push($term_value_array,$term_value);
    }

$term_name = "'" . implode("','", $term_value_array) . "'";

        $tax_query[] = array(
        'taxonomy' => $taxonomy_array_value,
        'field'    => 'slug',
        'terms'    => array($term_name)

    );


}
    $args = array(
    'post_type' => 'product',
    'tax_query' => $tax_query
    );

1 Answer 1

7

Here is one idea for a dynamic multiple taxonomy query:

// construct the tax-query: 
$tax_query = array( 'relation' => 'AND' );

// check if movie_genre taxonomy is to be included
if( $bMovieGenre ){
    $tax_query[] = array(
        'taxonomy' => 'movie_genre',
        'field'    => 'slug',
        'terms'    => array( 'action', 'comedy' )
    );
 }

// check if actor taxonomy is to be included
if( $bActor ){
    $tax_query[] = array(
        'taxonomy' => 'actor',
        'field'    => 'id',
        'terms'    => array( 103, 115, 206 ),
        'operator' => 'NOT IN'
    );
 }

$args = array(
    'post_type' => 'post',
    'tax_query' => $tax_query,
);

$query = new WP_Query( $args );

where $bActor and $bMovieGenre are constructed from the user input.

In general, if your user input array is like:

$selected = array( 
    array( 'taxonomy' => 'movie_genre' , 
           'terms'    => array( 'action' , 'comedy') ,
           'field'    => 'slug' ,
           'operator' => 'IN' ,
    ),
    array( 'taxonomy' => 'actor' , 
           'terms'    => array( 103, 115, 206 ) ,
           'field'    => 'slug' ,
           'operator' => 'NOT IN' ,
    ),
);

then you can try this to construct the dynamic taxonomy query :

$tax_query = array( 'relation' => 'AND' );

foreach( $selected as $sel ){

    $tax_query[] = array( 
                      'taxonomy' => $sel['taxonomy'] , 
                      'terms'    => $sel['terms'] , 
                      'field'    => $sel['field'] ,
                      'operator' => $sel['operator'] ,
                   );
}

or just

$tax_query = array( 'relation' => 'AND' );

foreach( $selected as $sel ){

    $tax_query[] = $sel; 

}
4
  • you are checking two taxonomies and taking two variables of taxonomy while I have multiple taxonomies and also don't know there name also terms are also in multiple number and dynamic coming from ajax. e.g I have array of taxonomy and corressponding terms array Commented May 26, 2013 at 11:26
  • I am using your code and making it dynamic with terms but it works for once only means when taxanoy = colors and terms = 'red' but when taxonomy = colors and terms = 'red','green' then it's not working Commented May 26, 2013 at 11:43
  • In the above example $tax_query[] is constructed from the user input, you should be able to adjust it to your needs. I updated the answer where you construct $selected from the user input.
    – birgire
    Commented May 26, 2013 at 11:54
  • 1
    Your updated code might have a problem with 'terms' => array($term_name), try instead 'terms' => $term_value_array.
    – birgire
    Commented May 26, 2013 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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