search

Hide Out of Stock Products in WooCommerce

You can hide out-of-stock products from catalog and search pages. The simplest way is through WooCommerce settings: WooCommerce > Settings > Products > Inventory > “Hide out of stock items from the catalog”. If you prefer a code-based approach, add to your functions.php. For more on managing stock in WooCommerce.

add_action( 'pre_get_posts', function( $query ) {
	if ( ! is_admin() && $query->is_main_query() && ( is_shop() || is_product_category() || is_product_tag() ) ) {
		$query->set( 'meta_query', array(
			array(
				'key'     => '_stock_status',
				'value'   => 'instock',
				'compare' => '=',
			),
		) );
	}
} );
Join the Discussion
0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!

Savvy WordPress Development official logo