Sorting Woocommerce Products Randomly


Without doubt, Woocommerce, a WordPress extension, is a great choice for developers who want to easily build an e-commerce site.

In this quick tip, we’ll explore how to create a Random sorting order for our products.

By default, Woocommerce arranges the products in one of the following ways:

  1. Default sorting (custom ordering + name).
  2. Popularity (sales).
  3. Average Rating.
  4. Sort by most recent.
  5. Sort by price (asc).
  6. Sort by price (desc).

But, wouldn’t it be better if we had the option to display our products randomly? Thankfully, we can do it by taking advantage of the code snippet below that WooThemes documentation provides us:

add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args' );

function custom_woocommerce_get_catalog_ordering_args( $args ) {
    $orderby_value = isset( $_GET['orderby'] ) ? woocommerce_clean( $_GET['orderby'] ) : apply_filters( 'woocommerce_default_catalog_orderby', get_option( 'woocommerce_default_catalog_orderby' ) );

    if ( 'random_list' == $orderby_value ) {
        $args['orderby'] = 'rand';
        $args['order'] = '';
        $args['meta_key'] = '';
    return $args;

add_filter( 'woocommerce_default_catalog_orderby_options', 'custom_woocommerce_catalog_orderby' );
add_filter( 'woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby' );

function custom_woocommerce_catalog_orderby( $sortby ) {
    $sortby['random_list'] = 'Random';
    return $sortby;

So, go ahead and add this code in the functions.php file of your theme. Next, navigate to the Settings page of your Woocommerce installation and hopefully you’ll see the new Random drop-down option:


Posted on June 5, 2015 in Know how by

A picture of George Martsoukos

George is an enthusiast web developer with passion about all the emerging web technologies.

Back to Top