WooCommerce Tutorials

Hide Out of Stock Products in Related Products for WooCommerce

Previously I wrote a post about hiding out of stock products in your WooCommerce store. One reader asked how they might go about hiding out of stock items only when viewing related products on the single product page. Well get ready, because here it is.

You can use a plugin like Code Snippets to add the following code to your website.

We’re basically toggling the “Hide out of stock items from the catalog” option found in WooCommerce > Settings > Products > Inventory using a filter. This means the option is never actually updated. Before the related products template is loaded we enable the option, and once it’s loaded and displayed, we disable the option.

You could also filter the related products query directly, using the same taxonomy query as explained in the original post, but this method seemed more reliable to me.

James is the founder of Iconic and an experienced WooCommerce plugin developer.



  1. makeonlineshop says:

    Thank you, I will try it and I have posted your page it here, hope it can help someone:


  2. jrotem says:

    Thank you! The solution on github kept throwing an error, but your code worked.

  3. Carret says:

    The problem is that not showing the ones that are out of stock does not always show all possible related products. Sometimes it shows 3, another 2, another 4, or 5, 1 … even if there are enough stock-related products.

    It seems that when looking for the configured number of products, if there are some without stock, discard them but not replace them with others.

    Should be a woocommerce problem, as I think the same thing happens if you mark the option in settings to not show products out of stock.

  4. Elizabeth Drew says:

    Thanks for this! Works exactly as promised from what I can see. I actually came across it as I am looking for a way to only show instock products on a certain page that uses a woocommerce shortcode. Pointing in the right direction for this would be much appreciated! Thanks again!

  5. Kenneth says:

    Hi, Thanks for sharing.
    We do you insert this snippet? I am looking at related.php

    $related = $product->get_related( $posts_per_page );

    if ( sizeof( $related ) === 0 ) return;

    $args = apply_filters( ‘woocommerce_related_products_args’, array(
    ‘post_type’ => ‘product’,
    ‘ignore_sticky_posts’ => 1,
    ‘no_found_rows’ => 1,
    ‘posts_per_page’ => $posts_per_page,
    ‘orderby’ => $orderby,
    ‘post__in’ => $related,
    ‘post__not_in’ => array( $product->id ),
    ) );

  6. Hi there,
    Will this also work with cross-sells?

    For some reason out of stock products show up in cross-sells in the cart area?!
    I guess I can go through every product each time something sells and update the cross-sells, but it would way more handy if I could add some code that’ll stop out of stock products from being displayed 🙂
    Alternatively is there a way to automatically select cross-sell products by category do you know?

    Thanks for your help!


  7. Domin says:

    The code does not work for me.
    Should the path “single-product/related.php” look different if a theme have the file in woocommerce/single-product/related.php? Is that change anything?

  8. Sarah says:

    It’s not compatible with Woocommerce v3.5.5 (Latest update) , and it doesn’t work at all; Would you update it please?

  9. MakeOnlineShops says:


    Possible to hide products from a specified category from Related products ?

    I have a category of products that are temporary not available and i want to hide these products from the related products block.

    Thank you.

  10. James says:

    I’m sorry but this doesn’t work properly. It hides the out of stock product, so related products will sometimes show 1,2,3 products instead of 4 (or whatever setting a user has).

  11. James says:

    I’m sorry but this doesn’t work properly. It hides the out of stock product, so related products will sometimes show 1,2,3 products instead of 4 (or whatever setting a user has).

  12. […] have followed this blog post, which a lot of other sites have as the solution to this problem, but it does not work properly (it […]

  13. Cody says:

    This snippit seems to be on the right track and is the only thing that has worked to hide related products, but it doesn’t seem to work as intended.

    As with other comments above, it causes empty spaces to appear where the sold products would have been.

    So if you are displaying 4 columns of Related Products, which displays 4 related items at random with each refresh of the page, if 2 of them would have been sold items being displayed, it just shows and empty space. It does not compensate for the sold items that are being hidden.

    It should be showing 4 random related items that are in stock with each refresh of the page.

    WooCommerce has a way to hide all sold products (via settings > products > inventory > Out of stock visibility) but it is too effective ensuring that ALL out of stock items completely disappear from everywhere. Sometimes you want control over where you hide them and where you show them. There has to be a way to tap into that setting.

  14. Andrei says:

    Is there a way to hide products on sale from related products?

  15. Andre says:

    is there anyway to apply this just to products in homepage? I have featured, recently added, category selected products randomly but i would like it to show only in stock.

  16. Mimmo says:


    I can hide cross-sell products in the page product if are not in stock?


  17. Dani says:

    As commented before, this plugin does the job. However, it is somewhat useless when it simply leaves blank spaces where the missing “out of stock” products were.
    Has somebody figured out how to display more products instead of the out of stock ones?

Leave a Reply

Your email address will not be published. Required fields are marked *