Home > Software design >  Hide out of stock products woocommerce query
Hide out of stock products woocommerce query

Time:02-11

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

CodePudding user response:

Try

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