2

i have product post type and i have product_cat is the taxonomy . In product_cat i have red, hard, soft, pen entires .

So i have to get the products coming under red and it coming hard or soft

How can i get this ?

For a product that come under both red and hard and soft i can use the following query

 $args = array(
        'post_status' => 'publish',
        'posts_per_page' => -1,
        'tax_query' => array(
          'relation' => 'AND',
                array(
                    'taxonomy' => 'product_cat',
                     'field' => 'slug',
                     'terms' => 'red'
                      ),
                     array(
                     'taxonomy' => 'product_cat',
                     'field' => 'slug',
                     'terms' => 'hard'
                      ),
                     array(
                     'taxonomy' => 'product_cat',
                     'field' => 'slug',
                     'terms' => 'soft'
                      )
                      ),
                     'post_type' => 'product',
                     'orderby' => 'title',
                     );

But what i need is red is must and either in soft or hard . ie (red && (soft||hard ))

Please help .

Abilash Erikson
  • 341
  • 4
  • 26
  • 55

1 Answers1

4

you can try this way:

'tax_query' => array(
        'relation' => 'AND',
        array(
            'taxonomy' => 'product_cat',
            'field'    => 'slug',
            'terms'    => array( 'red' ),
        ),
        array(
            'relation' => 'OR',
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'slug',
                    'terms'    => array( 'hard' ),
                ),
                array(
                    'taxonomy' => 'product_cat',
                    'field'    => 'slug',
                    'terms'    => array( 'soft' ),
                ),
        ),
    ),

Is untested, but it should work!

If not, here some useful links:

https://codex.wordpress.org/Class_Reference/WP_Query

https://10up.com/blog/2013/wordpress-mixed-relationship-taxonomy-queries/

FrancescoCarlucci
  • 1,570
  • 12
  • 13