Lyall
Lyall

Reputation: 1437

Exclude 'hidden' queried products from carousel in WooCommerce 3+

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?

Upvotes: 4

Views: 154

Answers (1)

LoicTheAztec
LoicTheAztec

Reputation: 253773

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

Upvotes: 4

Related Questions