Monday, May 9, 2022

[FIXED] How can I get/display WooCommerce bestselling products

Issue

I am developing a WooCommerce eBook store and I would like to display the list of best seller product on my home page.

How can I do that? can I do it with a custom WP_Query? Any other ways?


Solution

To get Best seller products, you can use different ways:

A WordPress WP_Query (limited here to 20 bestselling products):

$query_args =  array(
   'posts_per_page' =>  20, // -1 (for all)
   'post_type'      =>  array( 'product' ),
   'post_status'    =>  'publish',
   'meta_key'       => 'total_sales',
   'order'          => 'DESC',
   'orderby'        => 'meta_value_num',
);

$query = new WP_Query( $query_args );

if ( $query->have_posts() ) :
while ( $query->have_posts() ) : $query->the_post();

// Your product data
the_title(); // Output product title

endwhile;
wp_reset_postdata();

else :
// No post found

endif;

A WooCommerce Shordcode (the 20 best selling products):

[products best_selling limit="20"]

Documentation:



Answered By - LoicTheAztec
Answer Checked By - Gilberto Lyons (PHPFixing Admin)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.