WooCommerce: Change Number of Related Products

Here’s a simple snippet to change the display of Related Products on the single product page. Lately, I even added a fix for the Storefront theme, which already overrides the WooCommerce filter and needs a different call.

WooCommerce: Change Number of Related Products
WooCommerce: Change Number of Related Products

Snippet (PHP): Change Number of Related Products (Rows & Cols) – WooCommerce

 * @snippet       WooCommerce Change Number of Related Products
 * @how-to        Get CustomizeWoo.com FREE
 * @sourcecode    https://businessbloomer.com/?p=17473
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.5.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/

add_filter( 'woocommerce_output_related_products_args', 'bbloomer_change_number_related_products', 9999 );

function bbloomer_change_number_related_products( $args ) {
 $args['posts_per_page'] = 4; // # of related products
 $args['columns'] = 4; // # of columns per row
 return $args;

Additional CSS for Storefront Theme – Change Number of Related Products to 4

@media (min-width: 768px) {
.site-main .related.products ul.products li.product {
    width: 22%;
    float: left;
    margin-right: 4%;

Where to add custom code?

You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its style.css file. Make sure you know what you are doing when editing such files - if you need more guidance, please take a look at my guide "Should I Add Custom Code Via WP Editor, FTP or Code Snippets?" and my video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza.

36 thoughts on “WooCommerce: Change Number of Related Products

  1. The code still works, but I used flex for css.

  2. Fantastic – thank you! That’s one less plugin I need to install 🙂

    1. Yay!

  3. I need to do this with a conditional like:

    if ( has_term( 'cat_1', 'product_cat' ) || has_term( 'cat_2', 'product_cat' ) || has_term( 'cat_3', 'product_cat' ) ) {
    add_filter( 'woocommerce_output_related_products_args', 'bbloomer_change_number_related_products', 9999 );
    function bbloomer_change_number_related_products( $args ) {
     $args['posts_per_page'] = 9; // # of related products
     $args['columns'] = 3; // # of columns per row
     return $args;

    but it doesn’t work, Any suggestions?
    Thanks in advance!

    1. I have it, the conditional must be inside the function like this:

      add_filter( 'woocommerce_output_related_products_args', 'bbloomer_change_number_related_products', 9999 );
      function bbloomer_change_number_related_products( $args ) {
      if ( has_term( 'cat_1', 'product_cat' ) || has_term( 'cat_2', 'product_cat' ) || has_term( 'cat_3', 'product_cat' ) ) {
           $args['posts_per_page'] = 9; // # of related products
           $args['columns'] = 3; // # of columns per row
           return $args;

      In case it serves to someone 🙂

      1. Nice!

  4. How can I display 6 products in single row? It is showing 4+2 in 2 rows

    1. Hello Paul, thanks for your comment. If you entered “6” inside the snippet, then it’s your theme’s fault if that’s the result. You’ll need to add custom CSS to make the output show on 6 columns. Hope this helps

  5. Does this works on the cross sell on the cart page??

    1. Nope, sorry

  6. With WP core 5,1, woocommece 3.5.6 and storefront theme 2.4.3 the number of products shown are fix to 2. The CSS part works fine making room for more, but only two products are shown (from left). I have also Pootlepress Storefront Pro installed.

    1. Works for me 🙂 If you disable temporarily all plugins does it work?

  7. Today – feb 6th 2019 – the setting for the number of related items works, however, the column setting does not.
    Just #fyi

    1. Hey Han, thanks for your comment! Snippet works for me, so you must have a plugin/theme conflict 🙂

  8. Hello,

    Nice work Rodolfo congratulations. I would like to ask if it’s possible to have random related products from the same category and subcategory by using ‘orderby’ => ‘rand’, that kind of argument.

    Thank you in advance,


    1. Hello Nikos, thanks for your comment 🙂 “Orderby” = “rand” is already the default argument, check woocommerce_output_related_products(). so, it should already work like that 🙂

    2. Hello Rodolfo,

      Of course it is working like that by default. What it doesn’t do as it should or better as we want to do is showing related products from the same subcategory and not from the parent category.

      Thank you in advance,


      1. Ah, gotcha! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding! ~R

  9. Thank you SO much, man! You just saved my day 🙂

    All the best to you!

    1. Brilliant 🙂

  10. How do I change the no.of items per row for Up Sells and Cross sells please.

    1. Hello Geetha, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here via the blog comments. Thanks a lot for your understanding! ~R

  11. Wow, Jesse! Thank you very much for this!
    Since yesterday I am trying to get more than the usual 3 images to be displayed. Lot of testing with frustrating results. I didn’t expect Storefront to be the cause. If Google would’ve paid me for each single search I did the last days, I would be rich by now. 🙂 Luckily I decided to search for storefront specifically.

    Thank you again, all the best to you,


  12. Hi does this still work? Also, does it change the number of upsell products displayed from 3 to 4? I’m running the Divi theme.


    1. Hey Lee! Try adding a “9999” priority to the filter so it loads last (I revised the snippet). On top of that, your theme might require additional CSS to manage the WooCommerce columns (see Storefront example). Finally, this only changes the related products, for the upsells the code would be different. Hope this helps!

  13. Thumbs up! StoreFront snippet works like a charm!

  14. I have spent hours looking for a fix. I was trying to display 6 related products in 2 rows of 3 but it wouldn’t display with 3 per row. I would get 2 on one row and then 1 or 2 on the second row depending on how many characters there were in the excerpt. Your Related Products code has fixed this problem instantly. Thank you so much!

  15. Hi,

    one more time, amazing – including adaptation for storefront.

    Question: any idea how to make the related products displayed only stock products ?
    on my shop it shows almost only sold out products…

    Thanks a lot 🙂

    1. Ben, thanks so much for your comment! Yes, this is possible – but unfortunately this is custom work and I cannot provide a complementary solution here on the blog. Thanks a lot for your understanding! ~R

  16. Hey, any idea how to change the number of related products in StoreVilla theme ?

    Thanks in advance!

    1. No idea Weber 🙂 Have you asked your theme developers?

  17. Just FYI, it looks like your Storefront version is exactly the same as your other version except for the function name. I believe you’ll have to give the filter a priority of at least 11 to have it work.

    1. Jesse, spot on! Thanks so much for your comment, I must have forgotten the priority for some obscure reason 🙂 Have a great day!

Questions? Feedback? Support? Leave your Comment Now!

If you are writing code, please wrap it between shortcodes: [php]code_here[/php]. Failure to complying with this (as well as going off topic, not writing in English, etc.) will result in comment deletion. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining BloomerArmada to get blog comment reply priority, ask me 1-to-1 WooCommerce questions and enjoy many more perks. Thank you :)

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