WooCommerce

WooCommerce vs WP_Query: Get best selling products

By 0 Comments

To build a custom widget, shortcode or custom template, you want to get and display “best selling products” from 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,
	'posts_per_page'      => $posts_per_page,
	'meta_key'            => 'total_sales',
	'orderby'             => 'meta_value_num',
	'meta_query'          => $meta_query
);		

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