How do you exclude the out of stock products from WooCommerce products loop? In this tips I am sharing how you will exclude them from your shop, product category etc archive pages.
Open your functions.php file of your active theme and drop this simple PHP snippets at end of the file:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
function wpbb_exclude_outofstock_products( $query ) { if ( ( is_shop() || is_product_category() || is_product_taxonomy() ) && $query->is_main_query() && ! is_admin() ) { $args = array( 'relation' => 'AND', array( 'key' => '_stock_status', 'value' => 'outofstock', 'compare' => '!=' ) ); $query->set( 'meta_query', $args ); } } add_action( 'pre_get_posts', 'wpbb_exclude_outofstock_products', 90 ); |
Above code is excluding the out of stock items from shop page, product category/taxonomy archive pages.
Leave a Reply