"Shuffle" Sorting That Changes Daily
Adds a random order option to the shop sorting that stays the same all day, so pagination works and every product gets its turn at the top.
Opens WPIDE on
Code
$wpide_shuffle_default = false; // Make shuffle the default order.
add_filter( 'woocommerce_catalog_orderby', function ( $options ) {
$options['shuffle'] = 'Discover (random)';
return $options;
} );
add_filter( 'woocommerce_get_catalog_ordering_args', function ( $args, $orderby ) {
if ( 'shuffle' === $orderby ) {
$args['orderby'] = 'RAND(' . absint( current_time( 'Ymd' ) ) . ')';
$args['order'] = '';
$args['meta_key'] = ''; // phpcs:ignore WordPress.DB.SlowDBQuery
}
return $args;
}, 10, 2 );
if ( $wpide_shuffle_default ) {
add_filter( 'woocommerce_default_catalog_orderby', function () {
return 'shuffle';
} );
}
Notes
Set $wpide_shuffle_default to true to make it the default order for the shop.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.