Skip to content

Instantly share code, notes, and snippets.

@manchumahara
Created February 11, 2020 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save manchumahara/2afebd0d14790e77e1c93f74e7f5ab71 to your computer and use it in GitHub Desktop.
Save manchumahara/2afebd0d14790e77e1c93f74e7f5ab71 to your computer and use it in GitHub Desktop.
add_filter( 'woocommerce_shortcode_products_query',
function ( $query_args, $attributes, $type ) {
if ( $type == 'best_selling_products' ) {
unset( $query_args['meta_key'] );
unset( $query_args['meta_query'] );
add_filter( 'posts_clauses',
function ( $args ) {
$args['orderby'] = ' CASE wc_product_meta_lookup.min_price WHEN 0 THEN 1
ELSE -1
END ASC, wc_product_meta_lookup.total_sales DESC, wc_product_meta_lookup.product_id DESC
';
return $args;
} );
}
return $query_args;
}, 10, 3 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment