wp query with dynamic taxonomies and terms?

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 .

Read More

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

Related posts

1 comment

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

Comments are closed.