Add Random Sorting to WooCommerce Catalog

The woocommerce_catalog_ordering() function prints the “Default sorting” dropdown above the product loop. To add your own option to it, such as random sorting, you need two filters: woocommerce_catalog_orderby for the label and woocommerce_get_catalog_ordering_args for the query.

The example below adds a Random option. The same pattern works for any orderby value that WP_Query supports.

Add a Random Sorting Option

Add this to your child theme’s functions.php file or to a custom plugin:

/**
 * Add a "Random" option to the catalog sorting dropdown
 * and to the default sorting choices in the Customizer.
 */
function savvy_catalog_orderby_random( $options ) {
    $options['random_list'] = __( 'Random', 'your-textdomain' );
    return $options;
}
add_filter( 'woocommerce_catalog_orderby', 'savvy_catalog_orderby_random' );
add_filter( 'woocommerce_default_catalog_orderby_options', 'savvy_catalog_orderby_random' );

/**
 * Turn the "random_list" value into WP_Query arguments.
 */
function savvy_catalog_ordering_args_random( $args, $orderby, $order ) {
    if ( 'random_list' === $orderby ) {
        $args['orderby'] = 'rand';
    }
    return $args;
}
add_filter( 'woocommerce_get_catalog_ordering_args', 'savvy_catalog_ordering_args_random', 10, 3 );

Visit the shop page and pick “Random” from the dropdown. The URL gets ?orderby=random_list and the products come back shuffled.

How It Works

woocommerce_catalog_orderby filters the options array that woocommerce_catalog_ordering() renders. The key becomes the orderby value in the URL and the value is the label.

woocommerce_default_catalog_orderby_options filters the “Default product sorting” choices in Appearance > Customize > WooCommerce > Product Catalog. The Customizer validates against this list, so without it you cannot save Random as the default.

When the shop query runs, WC_Query::get_catalog_ordering_args() reads the value from the URL or the default setting. It splits the value on a hyphen (price-desc becomes price and DESC), then passes the result to woocommerce_get_catalog_ordering_args as $args, $orderby and $order.

Two details matter:

  • Use underscores in custom keys. A key like random-list would be split into random and LIST.
  • Compare against $orderby, not $_GET. The function is also called by the [products] shortcode and blocks with their own values, and the parameter reflects what is actually being sorted.

The woocommerce_catalog_ordering() Function

WooCommerce hooks woocommerce_catalog_ordering() to woocommerce_before_shop_loop at priority 30, right after the result count at 20. It loads the loop/orderby.php template, and only outputs when the loop is paginated and products will be shown.

These are the default options, before filters:

Key Label
menu_order Default sorting
popularity Sort by popularity
rating Sort by average rating
date Sort by latest
price Sort by price: low to high
price-desc Sort by price: high to low

The function then adjusts the list. It removes menu_order when the default sorting is set to something else, removes rating when reviews or star ratings are disabled, and on search results replaces menu_order with relevance.

There is no dedicated sorting shortcode. The dropdown appears in [products paginate="true"], and block themes render it with the Catalog Sorting block, which calls the same function.

For querying products in your own templates, see Properly Retrieve WooCommerce Products in Your Template.

Random Sorting and Pagination

ORDER BY RAND() reshuffles on every request. On page 2 some products from page 1 can appear again while others never show up.

WP_Query accepts a seeded RAND(x) since WordPress 4.5. A seed that changes once a day keeps the order stable across pages and page caches:

if ( 'random_list' === $orderby ) {
    $args['orderby'] = 'RAND(' . absint( gmdate( 'Ymd' ) ) . ')';
}

Random ordering also forces MySQL to sort the whole result set, which gets slow on catalogs with many thousands of products.

FAQs

Common questions about WooCommerce catalog ordering:

How do I remove the sorting dropdown in WooCommerce?
In classic themes, add remove_action( 'woocommerce_before_shop_loop', 'woocommerce_catalog_ordering', 30 ); to functions.php. In block themes, remove the Catalog Sorting block from the product catalog template.
How do I remove a default sorting option?
Filter woocommerce_catalog_orderby and unset() the key, for example unset( $options['popularity'] );, then return the array.
Why is my custom sorting option not showing?
The dropdown only renders on paginated product loops with products to display. Also check that your theme or a page builder is not printing its own sorting control instead of woocommerce_catalog_ordering().
Can I sort by a custom field?
Yes. Add the option with woocommerce_catalog_orderby, then in woocommerce_get_catalog_ordering_args set $args['meta_key'] to your field and $args['orderby'] to meta_value or meta_value_num. Products without that field drop out of the results.
Discussion
[  0 Comments  ]

Leave a Comment

To add code, use the buttons below. For instance, click the PHP button to insert PHP code within the shortcode. If you notice any typos, please let us know!