How to Hide Out of Stock Products
By default, when a product is out of stock it is still going to be displayed on your shop and product archive pages and there is going to be “Read more” button instead of “Add to cart”, like this:

But if you are reading this, I suppose you do not want them to be displayed, right? ;)
But wait, wait, there is also another option – we can display products in stock always first.
How to Show or Hide out of Stock Products in Settings
First things first – there is even no needs of using code snippets or any plugins in order to achieve that, you can just go to WooCommerce settings, Products tab > Inventory.

The products will still be available by the direct URL, which is great, because products could be bookmarked in the browsers. But the products won’t be displayed on shop page, product category and product tag archive pages, search results, related products, upsells and cross-sell sections.
Hide Out of Stock Products Programmatically
Products has a meta key _stock_status
which can be instock
or outofstock
(and on backorder
).
I saw an example on the internet, where a guy suggested to pass meta_query
parameter to the woocommerce_output_related_products_args
filter hook. I want to clarify one moment – this hook is not about passing args into WP_Query
. The filter hook supports only posts_per_page
, columns
, orderby
and order
parameters. That’s all!
So if you really need to hide your out of stock products from some specific places on the website and somehow the first method is not what you need, I recommend you to check pre_option_{option name}
filter hook.
/**
* Hide out of Stock Products in WooCommerce
*
* @author Misha Rudrastyh
* @link https://rudrastyh.com/woocommerce/hide-out-of-stock-products.html#programmatically
*/
add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', 'misha_hide_out_of_stock' );
function misha_hide_out_of_stock( $option ) {
if( is_admin() ) { // do nothing in WordPress admin
return $option;
}
$option = 'yes';
return $option;
}
As you can see in the above code, we excluded /wp-admin/, but you can also exclude any other pages using conditional tags. For example, let’s allow out of stock products to be displayed in search.
if( is_search() ) {
return $option;
}
And of course you can also do that with meta_query
and pre_get_posts
action hook, but please be careful, because this code may also be applied to your blog posts or anything else on the website.
/**
* Hide out of Stock Products in WooCommerce
*
* @author Misha Rudrastyh
* @link https://rudrastyh.com/woocommerce/hide-out-of-stock-products.html#programmatically
*/
add_action( 'pre_get_posts', 'misha_hide_out_of_stock_in_search' );
function misha_hide_out_of_stock_in_search( $query ){
// run the code only on the Shop page
if( is_shop() && $query->is_main_query() ) {
$query->set( 'meta_key', '_stock_status' );
$query->set( 'meta_value', 'instock' );
}
}
Related products – hide out of stock ones
This method may seem a little bit tricky, but it works perfectly.
/**
* Related Products – Hide out of Stock
*
* @author Misha Rudrastyh
* @link https://rudrastyh.com/woocommerce/hide-out-of-stock-products.html#related-products
*/
function misha_hide_out_of_stock_option( $option ){
return 'yes';
}
add_action( 'woocommerce_before_template_part', function( $template_name ) {
if( 'single-product/related.php' !== $template_name ) {
return;
}
add_filter( 'pre_option_woocommerce_hide_out_of_stock_items', 'misha_hide_out_of_stock_option' );
} );
add_action( 'woocommerce_after_template_part', function( $template_name ) {
if( 'single-product/related.php' !== $template_name ) {
return;
}
remove_filter( 'pre_option_woocommerce_hide_out_of_stock_items', 'misha_hide_out_of_stock_option' );
} );
- On line 1 you can see the function which always returns “yes” (hide out of stock products) no matter what option is set in WooCommerce settings
- We filter this option on line 11, but we create a filter only before our specific WooCommerce template part is included, that’s why it is inside an action
woocommerce_before_template_part
and there is also a condition on line 7. - Once the template is included, we don’t want this option to be filtered for the rest of the website page, so we remove the filter on line 21 and do it inside an action
woocommerce_after_template_part
- If you want to do the same for upsells, just replace template name on lines 7 and 17 to
single-product/up-sells.php
, for cross-sells in the Cart page –cart/cross-sells.php
.

Misha Rudrastyh
Hey guys and welcome to my website. For more than 10 years I've been doing my best to share with you some superb WordPress guides and tips for free.
Need some developer help? Contact me
I added the code to my functions.php to disable the ‘out of stock’ items in the related products section, but now it’s not showing any related products anymore. Also no items which are still for sale.
Do you know what could be the problem? Thanks in advance!
hi Misha,
nice code, i dont think peeps are reading so were and they have not relised that theres two functions displaying in first code functions section, prehaps define clearer for others.
You’re the best bro. i was looking for a solution like that, thank you so much!!