12

I have a product with attribute colors. Attribute values are red, blue and green. I am trying to create a custom search but I can't get the query to pull any product.

$args =  array(
    'post_type'      => array('product'),
    'post_status'    => 'publish',
    'posts_per_page' => -1,
    'meta_query'     => array( 
        array(
            'key' => '_visibility',
            'value' => array('catalog', 'visible'),
            'compare' => 'IN',  
        ) 
    ),
    'tax_query'      => array( 
        array(
            'taxonomy'        => 'product',
            'field'           => 'slug',
            'terms'           =>  array('blue', 'red', 'green'),
            'operator'        => 'IN',
        ),
    )
);

$products = new WP_Query( $args );

Where did I go wrong?

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
user3098629
  • 175
  • 1
  • 1
  • 13

1 Answers1

28

The correct taxonomy for the product attribute color is 'pa_color', so the correct working query is:

// The query
$products = new WP_Query( array(
   'post_type'      => array('product'),
   'post_status'    => 'publish',
   'posts_per_page' => -1,
   'meta_query'     => array( array(
        'key' => '_visibility',
        'value' => array('catalog', 'visible'),
        'compare' => 'IN',
    ) ),
   'tax_query'      => array( array(
        'taxonomy'        => 'pa_color',
        'field'           => 'slug',
        'terms'           =>  array('blue', 'red', 'green'),
        'operator'        => 'IN',
    ) )
) );

// The Loop
if ( $products->have_posts() ): while ( $products->have_posts() ):
    $products->the_post();
    $product_ids[] = $products->post->ID;
endwhile;
    wp_reset_postdata();
endif;

// TEST: Output the Products IDs
print_r($product_ids);

This code is tested and works. You will get all products that have Color attribute with the values (terms) 'blue', 'red' and 'green'…

Since WooCommerce 3, product visibility is handled by custom taxonomy product_visibility. You can see the following related threads:

Community
  • 1
  • 1
LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • Unfortunately, that is not working for me and I don't understand why. – user3098629 Aug 23 '17 at 17:06
  • @user3098629 I have test it on my test server, and for me it perfectly works. Anyway, the correct taxonomy for the attribute "Color" is `pa_color`. All product attributes taxonomy slugs start alway by `pa_` … – LoicTheAztec Aug 23 '17 at 17:10
  • Is there anything in the actual product that needs to be setup for this to work? I have a product that is visible and shows up everywhere else except this query. – user3098629 Aug 23 '17 at 17:40
  • 1
    I got it working....apparently the meta query was causing problems and causing it to return nothing. THanks for your help – user3098629 Aug 23 '17 at 17:50
  • Since Woocommerce 3 you no longer use `meta_query` to exlude hidden products, you will need to use `tax_query` detailed here https://wordpress.stackexchange.com/questions/231118/wp-query-exclude-hidden-products-from-woocommerce-product-list/262628#262628 – Rich S Jun 07 '19 at 19:14