How To Randomize Products On Category & Shop Page In 5 Minutes



I recent got a request from a friend of my to randomize the products display on his store’s categories. The reason is his site has so many categories and many of them have parent-child relationship. As a result, the display of many categories are identical.

Let me give you one example. My friend sells furniture. He has one category for indoor decoration (category A) and another for living room decoration (category B). A is B’s parent, obviously. When viewing the category page of B, you’ll see only B’s products. However, when viewing the category page of A, you’ll see category B’s products too. If B has enough products (more than the limit products per page), you may see category A’s page looks exactly like that of B.

Possible solution

We came up with two possible solutions. One is to manually setup the products for each category page. This is time consuming and requires more coding works. The other is to randomize the display of products on every category page so they don’t look the same.

We end up going for the 2nd solution since it’s faster and easier.

How to randomize products in category page

It turned out, the solution is very simple. Simply put the following code at the end of your theme’s functions.php file (make sure to use a child theme).

add_action('woocommerce_product_query', function($q){
	$q->set('orderby', 'rand');

Save the file and check your category pages. You should see the products are displayed in different order every time you refresh the page.

This works on shop page too.

What if you want to exclude shop page? If you want to keep the order on your shop page intact, read on.

How to exclude shop page products from being randomized

To exclude shop page, simply replace the code above with this:

add_action('woocommerce_product_query', function($q){
	if (is_shop())
		return $q;
	$q->set('orderby', 'rand');

As you can see, I added a conditional tag to exclude shop page. If you know PHP, the code above should be easy to understand.


Randomize products in category pages is an easy way to avoid duplication. This method is not ideal, of course. If you want to configure exact products for each category, you’ll need to get or build a plugin. However, for a quick solution, this one is good.

And my friend is happy.

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

Leave a Reply

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