Display products randomly by default on the shop page in WooCommerce

Display products randomly by default on the shop page in if using WooCommerce on your child site.

Snippet Type

Execute on Child Sites

Snippet

add_action( 'pre_get_posts', 'shop_default_orderby_rand' );
function shop_default_orderby_rand( $query ) {
    if ( is_shop() && ( ! isset($_GET['orderby']) || 'menu_order' === $_GET['orderby'] ) ) {
        $query->set( 'orderby', 'rand' );
    }
}
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.