WooCommerce: Custom Related Products

WooCommerce picks related products on the Single Product Page based on product categories and/or product tags. Related products are very important to the shopping experience, and sometimes this is not enough – what if you want to automatically show certain products based on different criteria?

So, here’s a quick snippet to e.g. get related products with the same product title of the current one. A very strange example, but you can use this as reference in case you want to get products based on different criteria. The get_posts() function, in fact, can be customized to get products with a given stock, certain price range, same custom field value, search term, and so on.

In this example, I’m getting Related Products by search string = “simple”

PHP Snippet: Custom Related Products

 * @snippet       Avoid Empty Cart Redirect @ WooCommerce Checkout
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 3.6.4
 * @donate $9     https://businessbloomer.com/bloomer-armada/

add_filter( 'woocommerce_related_products', 'bbloomer_related_products_by_title', 9999, 3 ); 

function bbloomer_related_products_by_title( $related_posts, $product_id, $args ) {
	$product = wc_get_product( $product_id );
	$title = $product->get_name();
	$related_posts = get_posts( array(
		'post_type' => 'product',
		'post_status' => 'publish',
		'title' => $title,
		'fields' => 'ids',
		'posts_per_page' => -1,
		'exclude' => array( $product_id ),
	return $related_posts;

Where to add this snippet?

You can place PHP snippets at the bottom of your child theme functions.php file (before "?>" if you have it). CSS, on the other hand, goes in your child theme 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 free video tutorial "Where to Place WooCommerce Customization?"

Does this snippet (still) work?

Please let me know in the comments if everything worked 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 on PHP 7+.

If you think this code saved you time & money, please join other Business Bloomer supporters and avail of 365 days of WooCommerce benefits. Thank you in advance :)

Need Help with WooCommerce Customization?

Check out these free video tutorials. You can start learning how to customize WooCommerce without unnecessary plugins. Watch me code and learn by example!

  • how-to-edit-woocommerce-with-php-snippets
  • woocommerce-hooks-add_action-list-visual
  • woocommerce-customize-single-product-page-PHP

Rodolfo Melogli

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as a 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.

6 thoughts on “WooCommerce: Custom Related Products

  1. Hi there,

    I added your code to my functions.php file. But now nothing (no related products) are showing up..

    Any advice?

    1. Hi Steff! Did you customize my snippet or use the exact same?

  2. I’ve been subscribing and asking questions on some of the modifications but my posts have been awaiting moderation since Noah built the ark. This is a nice site with lots of information but useless for getting questions answered.

    1. Hey Cor, sorry about that. I was off for 3 weeks plus it usually takes me 2 weeks to reply. I do my best ๐Ÿ™‚

  3. hello,

    The get_posts() function, in fact, can be customized to get products with a given stock, certain price range, same custom field value, search term, and so on.

    Thank you, but can you give another example ? Let’s say related products with the same price ?

    Your snippets are the best of the whole web ! thank you again !

    1. Hello there, thanks so much for your comment! Yes, this is definitely possible, but I’m afraid it’s custom work. If you’d like to get a quote, feel free to contact me here. Thanks a lot for your understanding!

Questions? Feedback? Support? Leave your Comment Now!

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

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