Jerielle
Jerielle

Reputation: 7520

Handle Woocommerce visibility for hidden products in a WP Query

Can you help me with this? I have a product list and what I want is to hide those products marked as hidden on the visibility detail of the product. Here's the code I have:

 $args = array(
                'posts_per_page' => -1,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'term_id',
                        // 'terms' => 'white-wines'
                        'terms' => $product_categories[$wc_arr_key[$wc_cat_id]]->term_id,
                        'visibility' => 'visible' //NOT WORKING...
                    )
                ),
                'post_type' => 'product',
                'orderby'    => 'menu_order',      
                'order'           =>  'ASC',      

            );
            $products = new WP_Query( $args );

            if(isset($_GET['staging']) && $_GET['staging'] == "true") {
                echo "<pre>" . print_r($products) . "</pre>";
            }

I want to display all the products that are marked as visible.

Upvotes: 1

Views: 3429

Answers (3)

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 use a second array in your tax query array:

$terms = array( $product_categories[$wc_arr_key[$wc_cat_id]]->term_id );

$products = new WP_Query( array(
    'post_type'         => 'product',
    'post_status'       => 'publish',
    'posts_per_page'    => -1,
    'tax_query'         => array(
        'relation'      => 'AND',
        array(
            'taxonomy'  => 'product_cat',
            'field'     => 'term_id',
            'terms'     => $terms
        ),
        array(
            'taxonomy'  => 'product_visibility',
            'terms'     => array('exclude-from-catalog'),
            'field'     => 'name',
            'operator'  => 'NOT IN',
        ),
    ),
    'orderby'           => 'menu_order',
    'order'             =>  'ASC',
) );


if(isset($_GET['staging']) && $_GET['staging'] == "true") {
    echo "<pre>" . print_r($products) . "</pre>";
}

Tested and works.

Related: Database changes for products in woocommerce 3

Upvotes: 2

WebguruInfosystems
WebguruInfosystems

Reputation: 87

WooCommerce save this data as metadata so you'll need to run a Meta Query against the name _visibility. This will look like

'meta_query' => array(
    array(
        'key'       => '_visibility',
        'value'     => 'hidden',
        'compare'   => '!=',
    )
)

This will pull all posts that do not have meta _visibility equal to hidden

Upvotes: 1

raju_odi
raju_odi

Reputation: 1455

Use below code to exclude hidden products and display only visible ones

$args = array(
                'posts_per_page' => -1,
                'tax_query' => array(
                    'relation' => 'AND',
                    array(
                        'taxonomy' => 'product_cat',
                        'field' => 'term_id',
                        // 'terms' => 'white-wines'
                        'terms' => $product_categories[$wc_arr_key[$wc_cat_id]]->term_id
                    )
                ),
                'meta_query' => array(
                    array(
                       'key'       => '_visibility',
                       'value'     => 'hidden',
                       'compare'   => '!='
                         )
                 ),
                'post_type' => 'product',
                'orderby'    => 'menu_order',      
                'order'           =>  'ASC'      

            );
            $products = new WP_Query( $args );

            if(isset($_GET['staging']) && $_GET['staging'] == "true") {
                echo "<pre>" . print_r($products) . "</pre>";
            }

Upvotes: 1

Related Questions