Are you looking for a way to display the best selling products in WooCommerce template?
The WooCommerce get best selling products code snippet I will share in this post should be a great place to start and help you display the best-selling products anywhere you want in your WooCommerce store.
WooCommerce Get Best Selling Products and Display
In this post, I will show you how to get the best-selling products in the default WordPress loop and display the products from the query and possibly create a shortcode that you can use in pages, templates, and posts of your WooCommerce shop.
First, it helps to understand that WordPress works with custom post types that allow users to create a custom way of displaying data. WooCommerce product is a custom post type.
With this in mind, you should now understand that it is possible to get the WooCommerce products using the default WordPress query from the database.
A simple WordPress query will look like shown in the code below :
$args = array( 'post_type' => 'product', 'orderby' => 'meta_value_num', 'meta_key' => 'price', ); $query = new WP_Query( $args );
This query argument will help you to display posts with the ‘Product’ type ordered by the ‘Price’ custom field.
Now we need to pay attention to the argument we have added in the args array – meta_key.
This will help us to get the custom field values and this is one of the easiest ways to query WooCommerce products.
WooCommerce Get Best Selling Products Query
In the query to get best selling products, we need to use the total_sales as the argument and the query should be as follows :
$args = array( 'post_type' => 'product', 'meta_key' => 'total_sales', 'orderby' => 'meta_value_num', 'posts_per_page' => 1, );
In the meta_key you can see we have placed the total_sales as our argument and this is what we will use to get the best selling products.
We can then loop through the best selling products data and display them in a template or create a shortcode that we can use to display in posts, pages, and in custom page templates as well.
The complete code snippet to get the best-selling products and display is as follows :
$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; wp_reset_query(); ?>
Conclusion
In this post, we have outlined how to get the best selling WooCommerce products and display them in a template. Ideally, you need to get the query and use the total_sales the meta_key argument, and that is all.
Joe is an experienced full-stack web developer with a decade of industry experience in the LAMP & MERN stacks, WordPress, WooCommerce, and JavaScript – (diverse portfolio). He has a passion for creating elegant and user-friendly solutions and thrives in collaborative environments. In his spare time, he enjoys exploring new tech trends, tinkering with new tools, and contributing to open-source projects. You can hire me here for your next project.
Similar Articles
- How to Use WooCommerce Product Attributes Step by Step [Complete Guide]
- Top 11+ Best E-commerce Platforms» Best Online Store Platforms for Small Business
- How to Use WooCommerce to Set Up Online Store
- How To Set Up Shipping Classes in WooCommerce
- How to Setup Free Shipping For Specific Products WooCommerce
- How to Delete a Category in WordPress
- How to Customize Product Category Page In WooCommerce
- How to Display Products by Category Shortcode WooCommerce
- How To Display Product Attributes In WooCommerce
- How to Get Product Description & Display WooCommerce
- How to Customize Storefront Theme Using Customizer
- How to Change Return to Shop Link in Woocommerce
- How to Add WooCommerce Products to a Page
- WordPress Get Post Category Snippet & Guide
- How to Get Product Brand Name in WooCommerce
- How to Delete a Page In WordPress
- How to Show Product Stock on the Cart Page WooCommerce
- How to Customize WooCommerce Order Received Page
- WooCommerce Redirect After Checkout: Redirect to Custom Thank You Page
- WooCommerce Redirect After Logout [Ultimate Guide]