Posted on

Hide Related Products by Category or Tag in WooCommerce

As you probably know, WooCommerce shows related products in the single product page and retrieves automatically them filtering products in the same categories and tags of the shown product.

This is a great feature in my opinion, but can be a double-edge sword when you have a lot of tags and/or categories in your shop, which ends to show unrelated products.

But the developers were smart and included two filters to allow you to filter by categories only or by tags only.

Open your functions.php file in wp-content/themes/your-theme-name/ and add this code at the end of the file:

The first snippet hides products by tag, while the second does the same for categories.

Obviously don’t use both of them together or you will not see any related products.

45 thoughts on “Hide Related Products by Category or Tag in WooCommerce

  1. Hey Nicola,

    I read your post on exclude products by tag or category, and another to exclude category. Would you show me how to display only products tagged “discount” to a logged in user, and only products tagged “retail” to a non-logged user?

    I’ve tried modifying some of your example code, but just not experienced enough to make it work. Thanks 🙂

    1. Hi,
      thanks for reading the post! I’ll write a tutorial for what you asked.

      Do you mean to show those products in the related products only if they have a specific tag and based on the logged in status of the user, right?

      1. Thanks for your reply and willingness to write a tutorial.

        Specifically, I mean products showing in the shop, but related products would apply too. If a user is logged in they would see only the products for which they are tagged, and if they are not logged in that user would see a different set of products for which they are tagged. In other words, tagged “wholesale” and “retail”.

      2. Hi, thanks for addressing this. I want to display related products by tag in woocommerce single product page . I put the code

        /**
        * Does not filter related products by category
        */
        add_filter( ‘woocommerce_product_related_posts_relate_by_category’, ‘__return_false’ );

        in themes functions.php but nothing changed.
        Is there anything additional i should be doing aside from above code?

        thanks,
        Dee

        1. No, you don’t need to add anything else. Be sure that the code is added correctly and maybe try with a different theme/no-plugins.

        2. That happened to me also. Just update the product after adding the code and saving the functions.php

        3. Same case with me. I added same code but nothing happened. Any pointers?

        4. Related products are also stored in transients, so wait until it expires or try to delete the transients from the System Status > Tools page.

  2. Hi Nicola — any further update on the tutorial?

  3. Hi Nicola —

    Thank you so much for the tutorial. After moving the file to theme and adding the code, I get the following error on the category page repeated for each product …themes\Encore\content-product.php on line 41 Warning: Invalid argument supplied for foreach() in. The error does not appear on the product page, only the category page.

    The error appears when I use the code exactly as sent and also when I replace ‘tag-slug’ with the desired tag.

    Again what I’m trying to accomplish on the category page is to show products tagged ‘retail’ to users that are not logged in — and products tagged ‘wholesale’ to users that are logged in. Each user will only see their tagged products.

    I appreciate your help and assistance.

  4. Hey Nicola — thank you again for your reply. I hope you will have a chance to advise on the error I reported ( …\themes\Encore\content-product.php on line 41 Warning: Invalid argument supplied for foreach() in). The code looks good, just not sure why the foreach error?

  5. Thank you for the update. Much appreciated as I was able to get it working for a logged in user (removing !).

    Would you be able to show me how to modify it to check both logged in and not logged users in with different tag excludes?

    Again, I appreciate your expertise and guidance.

  6. I was able to look at this again at the end of the day — and it appears the following (below) is working to filter product displays by user & tag. Any product not tagged displays as normal for both users.

    Thank you again so much for helping me reach a positive outcome.

    $product_tags = wp_get_post_terms( $product->id, 'product_tag' );
    
    if ( ! empty( $product_tags ) ) {
       foreach( $product_tags as $tag ) {
          if (  ( $tag->slug === 'retail' && is_user_logged_in() ) || ( $tag->slug ===' wholesale' && ! is_user_logged_in() )  ) {
             return;
          }
       }
    }
    
    1. Hi, that’s great! Thanks for letting me know and for sharing your code!

  7. Thank you so much. Simple but effective method.

    1. You’r welcome! 🙂 Thanks for reading!

  8. Hi! I came across your post for removing the related products. This is what I was looking for because I have carefully planned out the products I want to sell via the linked products section. However, I am using a Woo Commerce plugin, not an actual theme. I cannot find the file you mentioned in your post. Can you please tell me what file or files I need to edit to accomplish this task? (I’m looking to remove the related products from everything and not show up at all!) Thanks for your help!

    1. Hello, thanks for reading.

      It’s impossible that you don’t have the file functions.php. Without it your site won’t work. Probably you are in the wrong folder.

      From the root of your WordPress installation go to wp-content/themes/your-theme-name/ where your-theme-name is the name of your theme (e.g. if you are using the theme Storefront the folder name would be storefront). Inside it you must find the file functions.php

  9. This is brilliant, I have searched through many websites and tried out with different “solutions”, but only this work perfectly.

    1. Awesome! Thanks for reading 🙂

  10. Its work with me. Really thank man. But if wp / theme get update. Will we lose the change ? And if we lose, we need to add this code again ?
    Btw. thank you for hard work 🙂

    1. Hi, awesome!

      Yeah you will lose your changes if you don’t use a child theme: https://codex.wordpress.org/Child_Themes

  11. Hey Nicola,
    thanks for your code-snippets. I actually searched for this. Unfortunately, when I insert this code in my functions.php (I don’t use a child theme) nothing happens. Even if I insert all two variations;

    It concerns the following website: http://www.alpenpix.at.

    Can you help?

    Thanks 🙂

    1. Hi Patrick,
      did you try to change the theme and disabling plugins?

  12. Hi Nicola,

    I like your code and think it is something really close to what we are after. Would you know how we could make the related products to be drawn from the immediate category only?

  13. I’ve got products on my site with:

    Category > sub category A > sub category B

    I’m trying to match products only in sub category B. Any tips?

    1. Hello Tommy,
      You can read my comment here. It would be the same for categories.

  14. Perfect! Just what I was looking for. THANK YOU!

  15. Hi, I have added this using a Code Snippets plugin and it is not working. I am not using a child theme and I do not want to lose it on updates. Should I be able to use it as a snippet in the plugin?

    1. I have been going through the other issues and realised if I update the products again (as someone mentioned above for something else) it works.

      1. Yeah, that happens because related products are stored in a transient.

        This will have impact on the performance of the website, so we store it there. If you want, just update the product to update the transient with the new related products.

  16. Hi, a useful post 😀 But I have a question, is there a way to filter related products by tag but exclude some tag (will not show related products with a certain tag

    For example:

    Products include the tag: product 2014, product 2015
    -Then it will just display the related products in the tag: product 2015

    1. Hi Trieu,
      Thanks for your comment!

      That is definitely possible, but you need to use a different filter. From this article, just exclude categories. Then you need another function and filter to filter by tag.

      You will have to edit the arguments used to get related products by using the filter woocommerce_related_products_args. It passes an array with arguments used by WP_Query.

      In that array, you can add the tax_query index and build your own query to filter by tags. You can find the code I’m talking about here.

      Please read the documentation about tax_query here.

  17. This worked perfect. Thanks so much.

  18. I am looking for exacetly the same as Trieu, I would like to filter the related products to show only related products with a specific tag or category. Unfortunately, I dont understand your answer, could you please provide the exact code? Many thanks in advance and best wishes!

  19. Unfortunately it doesn’t work for me.

  20. Will you be able to help?

    1. Maybe! Do you receive any error? Did you try with a different theme?

  21. Hi, I am new to woocommerce. I’m working on a gun range / shop website that was developed by someone else. Currently, there are a lot of categories “Used guns”, “New Guns” as well as “Pistol”, “Shotgun” etc. The problem is when you view a Shotgun, you may see a Pistol as a related product just because they both are tagged as Used Guns. If I add different caliber categories for each type of firearm, is there a way to have it to where if you are looking at a Pistol that is a certain caliber, that the “Related Products” only show other Pistols of that same caliber? Thanks in advance for your help.

    1. Hello Michael,
      It is possible, but this article won’t help you.

      You will have to use a different filter to customize the arguments of the categories retrieved for related products and get only products from the same Main category or something (not sure how your shop is configured).

      The filter to use is woocommerce_get_related_product_cat_terms and it’s from this file.

  22. Hi, thanks for this post, but i have a doubt… my products are related by tags and i have in my file function.php this code (
    add_filter(‘woocommerce_product_related_posts_relate_by_category’, function() {return false;}); ) but i don´t want that show the products of the same category, i don´t know if exist a query for this or filter that exclud products of the same category for related products, thanks for your help and attention.

Leave a Reply

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