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.

52 replies
  1. rrosenthal
    rrosenthal says:

    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 🙂

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      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?

      Reply
      • rrosenthal
        rrosenthal says:

        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”.

        Reply
      • dee
        dee says:

        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

        Reply
  2. rrosenthal
    rrosenthal says:

    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.

    Reply
  3. rrosenthal
    rrosenthal says:

    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?

    Reply
  4. rrosenthal
    rrosenthal says:

    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.

    Reply
  5. rrosenthal
    rrosenthal says:

    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;
          }
       }
    }
    
    Reply
  6. Alexis
    Alexis says:

    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!

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      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

      Reply
  7. bluemoon
    bluemoon says:

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

    Reply
  8. Hung Le
    Hung Le says:

    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 🙂

    Reply
  9. Patrick
    Patrick says:

    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 🙂

    Reply
  10. Luke
    Luke says:

    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?

    Reply
  11. Tommy Picard
    Tommy Picard says:

    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?

    Reply
  12. Kate
    Kate says:

    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?

    Reply
    • Kate
      Kate says:

      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.

      Reply
      • Nicola Mustone
        Nicola Mustone says:

        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.

        Reply
  13. Trieu
    Trieu says:

    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

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      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.

      Reply
  14. Lina
    Lina says:

    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!

    Reply
  15. Michael
    Michael says:

    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.

    Reply
    • Nicola Mustone
      Nicola Mustone says:

      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.

      Reply
  16. Noemi Ceron
    Noemi Ceron says:

    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.

    Reply
  17. Anton
    Anton says:

    Hello!
    I dont know why, but
    add_filter( ‘woocommerce_product_related_posts_relate_by_tag’, ‘__return_false’ );
    dont work in my case. I use WC 3.0.6

    Can you help?

    Reply
  18. Matthew
    Matthew says:

    It seems that the default related products is category OR tags. Is there a way to make it AND so that in order to a product be shown in the related products list, it must be in the same category AND have the same tag? This would actually make it more related.

    Reply
  19. anddy
    anddy says:

    You have a helpful blog!! Great Job!

    How would I approach if I want to hide all related products for single category?

    There is something on my website that I dont want to show any related product.

    I found some codes online but none of them worked :/

    Reply
  20. Deepa
    Deepa says:

    Hello,

    I want two related product slider one based on categories and another one should be based on tags. I am new to woo commerce, if any one could help me with this it would be very help ful

    thank in advance

    Reply

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

You have to agree to the comment policy.

This site uses Akismet to reduce spam. Learn how your comment data is processed.