I would like to consider best-selling product in this loop :
\'product\', \'posts_per_page\' => 1, \'product_
I had a hard time trying to get a list of best selling product through the standard loop. But none of the solutions from here and other stackoverflow posts with the same problem do not work for me. Finally I get result that solve my problem through the custom query to data base.
global $wpdb;
$order_totals = $wpdb->get_results( "SELECT kp_posts.ID FROM kp_posts INNER JOIN kp_postmeta ON ( kp_posts.ID = kp_postmeta.post_id ) WHERE 1=1 AND ( kp_postmeta.meta_key = 'total_sales' ) AND kp_posts.post_type = 'product' AND (kp_posts.post_status = 'publish') GROUP BY kp_posts.ID ORDER BY kp_postmeta.meta_value+0 DESC, kp_posts.post_date DESC LIMIT 0, 16" );
foreach ( $order_totals as $value ) {
$product = wc_get_product( $value->ID );
echo $product->get_title();
}
Solution 1: Add This plugin WP woocommerce best selling products by category
Solution 2: For retrieving the best buying products in WooCommerce we are using wp_query manipulation with meta_key as “total_sales” and orderby “meta_value_num”. In short we are displaying products as per the total sales number.Here is the code for this
<?php
$args = array(
'post_type' => 'product',
'meta_key' => 'total_sales',
'orderby' => 'meta_value_num',
'posts_per_page' => 1,
);
$loop = new WP_Query( $args );
while ( $loop->have_posts() ) : $loop->the_post();
global $product; ?>
<div>
<a href="<?php the_permalink(); ?>" id="id-<?php the_id(); ?>" title="<?php the_title(); ?>">
<?php if (has_post_thumbnail( $loop->post->ID ))
echo get_the_post_thumbnail($loop->post->ID, 'shop_catalog');
else echo '<img src="'.woocommerce_placeholder_img_src().'" alt="product placeholder Image" width="65px" height="115px" />'; ?>
<h3><?php the_title(); ?></h3>
</a>
</div>
<?php endwhile; ?>
<?php wp_reset_query(); ?>