WooCommerce PHP Beginner Requires WooCommerce

Change the Number of Related Products

Show more or fewer related products on product pages, and set how many columns they use.

Tested on WordPress 7.1 PHP 8.3 Updated
functions.php 5 lines
add_filter( 'woocommerce_output_related_products_args', function ( array $args ): array {
	$args['posts_per_page'] = 3; // How many products.
	$args['columns']        = 3; // How many per row.
	return $args;
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

WooCommerce shows four related products by default. If your layout fits three across, or you want a longer row, this snippet changes the count and the columns.

How it works

The woocommerce_output_related_products_args filter receives the arguments WooCommerce uses to fetch and display related products. posts_per_page sets how many to show and columns sets how many per row.

Things to watch

  • If a product has few siblings in its categories and tags, WooCommerce may show fewer than you ask for.
  • This only affects the classic related products section. Block themes set the count in the Related Products block.

How to test it

Open a product page and count the related products.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only