"Most Reviewed" Sorting Option
Adds a "Sort by most reviewed" choice to the shop so shoppers can find the products other customers talk about most.
Opens WPIDE on
Code
add_filter( 'woocommerce_catalog_orderby', function ( $options ) {
$options['reviews'] = 'Sort by most reviewed';
return $options;
} );
add_filter( 'woocommerce_get_catalog_ordering_args', function ( $args, $orderby ) {
if ( 'reviews' === $orderby ) {
$args['orderby'] = array( 'meta_value_num' => 'DESC', 'date' => 'DESC' );
$args['order'] = 'DESC';
$args['meta_key'] = '_wc_review_count'; // phpcs:ignore WordPress.DB.SlowDBQuery
}
return $args;
}, 10, 2 );
Notes
Uses the review count WooCommerce already stores, so it adds no extra work per page.
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.