WooCommerce vs WP_Query: Get top rated products

By 0 Comments

I’m using already designed theme for wordpress, and now instead of regular blog posts I would like to display WooCommerce products with order by rating?

Other question: I’m try to get top rated products, but i can’t do it, help me !?

Don’t worry!

To build a custom widget, shortcode or custom template, you want to get and display “top rated products” form Woo Commerce. You can try my code.

$posts_per_page = 10;

$meta_query = WC()->query->get_meta_query();

$atts = array(
	'orderby' => 'title',
	'order'   => 'asc');

$args = array(
	'post_type'           => 'product',
	'post_status'         => 'publish',
	'ignore_sticky_posts' => 1,
	'orderby'             => $atts['orderby'],
	'order'               => $atts['order'],
	'posts_per_page'      => $posts_per_page,
	'meta_query'          => $meta_query

add_filter('posts_clauses', array( 'WC_Shortcodes', 'order_by_rating_post_clauses'));

$products = new WP_Query(apply_filters('woocommerce_shortcode_products_query', $args, $atts));

remove_filter( 'posts_clauses', array( 'WC_Shortcodes', 'order_by_rating_post_clauses' ) );