If you have a WooCommerce shop, you’ve probably noticed something annoying: when customers click “Sort by popularity,” the stuff that shows up first are usually the old products that sold a ton… three years ago. Meanwhile, the new thing everyone’s buying right now is buried on page 5. Super frustrating, right?
Customers want to see what’s hot TODAY, not what grandma bought in 2021.
So here’s the good news: we can fix that really easily and make “Sort by popularity” actually mean “what’s selling like crazy in the last 6 months.” Way more useful!
Are you in need of a skilled WordPress developer to bring your website vision to life?
Look no further! Whether you need custom themes, plugin development, site optimization, or ongoing support, I offer expert WordPress development services to suit your needs.
Why 6 months is better than lifetime sales
Picture this:
- You launch a cool new t-shirt, and boom — 200 people buy it in the first 3 weeks.
- You have an old t-shirt that sold 400 times… but over the last 3 years, and barely any lately.
Right now, WooCommerce thinks the old one is more “popular” and shows it first. That’s dumb. The new one is clearly the hot item right now!
When you switch to last-6-months sales:
- The stuff that’s actually trending jumps to the top
- Your category pages feel fresh and exciting
- Customers see what everyone else is buying right now (people love that)
- You sell more → happy dance
Suggested Read: How to Add a Buy 2 Get 1 Free (BOGO) Offer in Woocommerce
How WooCommerce does popularity right now
By default, WooCommerce looks at a number called total_sales that it keeps forever (it’s stored in the database). Every time someone buys something, it adds +1 to that product’s lifetime counter. That’s why ancient products always win.
We’re going to cheat a little (in a good way) and overwrite that number with “sales only from the last 6 months.” WooCommerce won’t know the difference, and suddenly the sorting works exactly how we want!
I’ll show you the super simple code for that in the next part – takes 2 minutes to add and works like magic.
Most Popular Plugin: Install WooBooster Partial COD Plugin to improve your store ROI
Replacing WooCommerce Popularity with Last 6 Months Sales
Use the following code in your functions.php or a custom plugin:
add_filter( 'woocommerce_get_catalog_ordering_args', 'change_default_popularity_sorting_6_months' );
function change_default_popularity_sorting_6_months( $args ) {
// Only affect when sorting by popularity
if (!isset($_GET['orderby']) || ( isset($_GET['orderby']) && $_GET['orderby'] == 'popularity' ) ){
$args['orderby'] = 'meta_value_num';
$args['order'] = 'ASC';
$args['meta_key'] = '_last_6_month_sales';
}
return $args;
}
add_action( 'admin_init', 'tw_convert_popularity_to_last_6_months' );
function tw_convert_popularity_to_last_6_months() {
if ( get_option( 'tw_last_6m_sales_complete' ) ) {
return; // Already processed, skip
}
global $wpdb;
$date_from = date( 'Y-m-d', strtotime('-6 months') );
// Get last 6 months qty sold per product
$results = $wpdb->get_results("
SELECT im.meta_value AS product_id, SUM( oim.meta_value ) AS qty_sold
FROM {$wpdb->prefix}woocommerce_order_items oi
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta oim
ON oi.order_item_id = oim.order_item_id
INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta im
ON oi.order_item_id = im.order_item_id
INNER JOIN {$wpdb->posts} p
ON oi.order_id = p.ID
WHERE p.post_status IN ('wc-completed','wc-processing')
AND oim.meta_key = '_qty'
AND im.meta_key = '_product_id'
AND p.post_date >= '{$date_from}'
GROUP BY product_id
");
$lookup = $wpdb->prefix . 'wc_product_meta_lookup';
// Reset all totals to 0 first
$wpdb->query( "UPDATE {$lookup} SET total_sales = 0" );
// Update calculated sales
foreach ( $results as $row ) {
$wpdb->update(
$lookup,
[ 'total_sales' => $row->qty_sold ],
[ 'product_id' => $row->product_id ],
[ '%d' ],
[ '%d' ]
);
update_post_meta( $row->product_id, '_last_6_month_sales', $row->qty_sold ); // optional display
}
update_option( 'tw_last_6m_sales_complete', time() );
}
What Happens After Running This Code?
- WooCommerce automatically treats the last 6 months sales as total sales
- Sorting by popularity now shows trending products first
- No need to change the dropdown or template design
- Works on shop, category, related products, upsells, widgets and search results
Also Read: Partial Cash on Delivery (Partial COD): A Smart Payment Method for Your Online Store
Optional: Show Last 6 Months Sales Count Under Product Title
add_action( 'woocommerce_after_shop_loop_item_title', function() {
global $product;
$v = get_post_meta( $product->get_id(), '_last_6_month_sales', true );
if ( $v ) echo 'Sold (Last 6 Months): '.$v.'
';
}, 15 );
Final Words
Sorting based on recent popularity gives your store a smarter product ranking system. It improves customer experience, increases visibility for trending items, and ultimately leads to better conversions.
Instead of depending on lifetime sales, which may be outdated, showing what customers are buying right now encourages trust and encourages new buyers.
Want More Enhancements?
If you want, I can help you with:
Are you in need of a skilled WordPress developer to bring your website vision to life?
Look no further! Whether you need custom themes, plugin development, site optimization, or ongoing support, I offer expert WordPress development services to suit your needs.
Thanks for reading 🙏, I hope you found How to Sort WooCommerce Products by Popularity Based on Last 6 Months Sales tutorial helpful for your project. Keep learning! If you face any problems – I am here to solve your problems.

Pradeep Maurya is the Professional Web Developer & Designer and the Founder of “Tutorials website”. He lives in Delhi and loves to be a self-dependent person. As an owner, he is trying his best to improve this platform day by day. His passion, dedication and quick decision making ability to stand apart from others. He’s an avid blogger and writes on the publications like Dzone, e27.co
