FoamyMedia
FoamyMedia

Reputation: 496

Hide out of stock products woocommerce query

Tying to hide out of stock products from a query

<?php
                $args = array( 
                        'post_type' => 'product', 
                        'posts_per_page' => 16, 
                        'product_cat' => 'crisps', 
                        'orderby' => 'rand',
                        'meta_query' => array (
                            'key' => '_stock_status',
                            'value' => 'instock'
                        )
                    );
                $loop = new WP_Query( $args );
                if ( $loop->have_posts() ) {
                    while ( $loop->have_posts() ) : $loop->the_post();
                        wc_get_template_part( 'content', 'product' );
                    endwhile;
                } else {
                    echo __( 'No products found' );
                }
                wp_reset_postdata();
                ?>

however the out of stock products are still showing

any help

Upvotes: 0

Views: 829

Answers (1)

Bazdin
Bazdin

Reputation: 1069

Try

'meta_query' => array (
        array(
            'key' => '_stock_status',
            'value' => 'instock'
        ),
    )

Upvotes: 4

Related Questions