4

I have a carousel plugin which does various things and it only shows published products:

$common_args = array(
            'post_type' => 'product',
            'posts_per_page' => !empty($posts_per_page) ? intval($posts_per_page) : 4,
            'post_status' => 'publish',
            'ignore_sticky_posts' => true,
            'no_found_rows' => true,

        );

But I need it to exclude 'hidden' products, which technically are still published just not visible. Alternatively I could use it if it excluded products that were in specific categories (all my hidden products are in two specific categories).

How can I do either of these please?

LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275
Lyall
  • 1,158
  • 3
  • 13
  • 34

1 Answers1

4

Since Woocommerce 3 the product visibility is handled by the taxonomy product_visibility for the term exclude-from-catalog, so you need to add a tax query as follow:

$common_args = array(
    'post_type'           => 'product',
    'posts_per_page'      => !empty($posts_per_page) ? intval($posts_per_page) : 4,
    'post_status'         => 'publish',
    'ignore_sticky_posts' => true,
    'no_found_rows'       => true,
    'tax_query'           => array( array(
        'taxonomy'  => 'product_visibility',
        'terms'     => array('exclude-from-catalog'),
        'field'     => 'name',
        'operator'  => 'NOT IN',
    ) ),
);

It should work. Tested this array of arguments with WordPress get_post() function (it works).


Related: Database changes for products in woocommerce 3

LoicTheAztec
  • 184,753
  • 20
  • 224
  • 275
  • @Lyall I have tested that on a `WP_Query` using `get_posts()` and it works perfectly… – LoicTheAztec Apr 25 '19 at 18:03
  • 1
    You're right it works perfectly, I just had to troubleshoot the issue in my particular set-up. All sorted, thank you for the quick answer :) – Lyall Apr 25 '19 at 18:10