whb-logo8whb-logo8whb-logo8whb-logo8
  • Home
  • About
    • About
    • Contact
  • Blog
  • Methodology
  • Affiliate Disclosure
    • Disclaimer
    • Privacy Policy
✕

WooCommerce – Default Sort Method for a Specific Category

WooCommerce is an amazing WordPress plugin, but some of the default sorting options are not that robust. Sure, you can change the default sorting category, but this is applied globally and doesn’t help with category-specific sorting methods.

One of the biggest problems everyone has is this: what if you want to want to change the default sorting option, but only for a specific category or categories? Well, currently there’s no plugin to accomplish this, and no functionality in WordPress to do it either.

To add to the problem, there are tons of blogs where people post ways to make custom sorting options, remove sorting options, etc., but no one freaking explains how to have the default WooCommerce sorting method change based on the categories.

There are pages and pages of unanswered comments online on blogs everywhere, so—enter Web Hosting Buddy to the rescue! We whipped up some code to make this happen and it’s been tested/verified.

Here’s the code:

//custom function to override default sort by category
function custom_default_catalog_orderby() {

//choose categories where default sorting will be changed
if (is_product_category( array( 'category1', 'category2', 'category3' ))) {
	return 'date'; // sort by latest
	
}else{

return 'popularity'; // sort by popularity as the default
	
  } // end if statement
} //end function

add_filter( 'woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby' ); //add the filter

In this example, we apply the “Sort by latest” sorting method (which is by most recent date) to category1, category2, and category3. Then, all other categories are sorted by popularity in the else statement.

Now, we’ll break it down and explain what the code is doing…

First, we create a custom function called custom_default_catalog_orderby.

//custom function to override default sort by category
function custom_default_catalog_orderby() {

Next, we have a conditional if statement for WooCommerce to check the product categories. We used 3 example categories. This requires a PHP if conditional statement and an array for multiple categories.

//choose categories where default sorting will be changed
if (is_product_category( array( 'category1', 'category2', 'category3' ))) {

Then, on these category pages we return the “Sort by latest” sorting method as default. WooCommerce calls this “date” in their PHP variables. If you’re modifying this, it’s important to note that variables used to call the sort methods aren’t the same as the drop-down GUI names.

return 'date'; // sort by latest

WooCommerce refers the GUI menu name “Sort by latest” as “date” in their PHP variables. If you’re modifying this and sorting by a different method, it’s important to note that the variables are as follows:

popularity = “Sort by popularity”
rating = “Sort by average rating”
date = “Sort by latest”
price = “Sort by price: low to high”
price-desc = “Sort by price: high to low”

With that said, let’s get back to the code…

Next, we have an else statement that returns popularity for all the other pages not checked in the above code (everything except category1, category2, and category3). This is important, because if you don’t have this section of code, you won’t be able to set a default sort for all of the other categories in WooCommerce settings because this code overrides everything.

}else{

return 'popularity'; // sort by popularity as the default
 
  } // end if statement
} //end function

Finally, we have to add the filter to apply our code. Filters in WordPress allow hooks into plugins that modify data at runtime.

The filter that we’re calling is the “woocommerce_default_catalog_orderby” filter, which overrides the default order configured in the WooCommerce settings.

add_filter( 'woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby' ); //add the filter

Now, what if you only want to apply a default sorting method to only one category instead of three? In that case, you’d remove the array and have a single category like this:

//choose categories where default sorting will be changed
if (is_product_category( 'category1' )) {

And that’s it! If you’re looking to create a custom sorting method within the dropdown menu, the WooCommerce support docs have a great reference for that here:

https://docs.woocommerce.com/document/custom-sorting-options-ascdesc/

And, if you need to reference the WooCommerce conditional tags, you can find those here:

https://docs.woocommerce.com/document/conditional-tags/

Hopefully now you’ll be able to create some great customs sorting for your WooCommerce product pages/categories!

Related posts

July 27, 2020

WooCommerce Part 5 – How to Exclude Products in a Specific Category from WooCommerce Search


Read more

Looking for a Good WooCommerce Host?

We recommend these hosts for WooCommerce awesomeness!

Hostinger

SiteGround

WP Engine

Hey you! A quick heads up: if you happen to buy hosting via one of the links on this page (affiliate links), we might get a commission. This helps support the site, and keeps the awesome content coming free of charge—and we really appreciate that! 🙂

Guides & Reviews

  • The Best WordPress Hosting – An Expert Guide
  • The Best Web Hosting for Small Businesses
  • A Guide to Web Hosting for Nonprofits
  • The Best Domain Name Registrars
  • The Ultimate Guide to Web Hosting Alternatives
  • Web Hosting Glossary
  • Web Hosting How-Tos & Tutorials
  • Best Web Hosting & Software

Comparisons

  • The Fastest Web Hosting - Find Out Who Wins!
  • A Big List of Web Hosting Companies
  • Bluehost vs Hostgator
  • Dreamhost vs Hostgator
  • Bluehost vs Godaddy
  • Siteground vs Hostgator
  • Bluehost vs Dreamhost
  • The 5 Best GoDaddy Alternatives
  • Top 12 JMeter Alternatives
  • 20 Best Load Testing Tools
  • Hosts We Don't Recommend

Deals & Savings

  • Bluehost Coupon Codes & Discounts
  • Hostinger Coupon Codes & Deals
  • SiteGround Coupon Codes & Deals
  • DreamHost Coupon Codes & Discounts
  • HostGator Coupon Codes & Discounts
  • WPEngine Coupon Codes & Promos
  • NameSilo Coupon Codes & Deals
  • Web Hosting Coupons & Deals
  • Cheap Web Hosting
  • Cheap Email Hosting
✕
Follow @WebHostBuddy
Copyright 2025 WebHostingBuddy.com. All rights reserved.