Skip to content

Instantly share code, notes, and snippets.

@mikejolley
Created January 16, 2012 18:54
Show Gist options
  • Star 36 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save mikejolley/1622323 to your computer and use it in GitHub Desktop.
Save mikejolley/1622323 to your computer and use it in GitHub Desktop.
WooCommerce - Change default catalog sort order
/**
* This code should be added to functions.php of your theme
**/
add_filter('woocommerce_default_catalog_orderby', 'custom_default_catalog_orderby');
function custom_default_catalog_orderby() {
return 'date'; // Can also use title and price
}
@sashareds
Copy link

A client has some quirky feature request. Having one particular product going always first and the rest products are in alphabetical order..
Any ideas on that?

@cipriantepes
Copy link

^
using @jatinsapra, update these

$args['meta_key'] = '_featured';
$args['order'] = 'desc';

Cheers!

@peariewang
Copy link

I use this one for random, and it works, thanks

add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
$args['meta_key'] = '';
$args['orderby'] = 'rand';
$args['order'] = 'rand';
return $args;
}

@limsocheat
Copy link

Thanks.

@jpcmf
Copy link

jpcmf commented Jun 30, 2015

Thanks.

@nickzamboli
Copy link

If you want the catalog ordering to still work properly you should wrap your code in something like:

function woocommerce_catalog_orderby( $args ) {
if(!$_GET['orderby']) {
$args['orderby'] = 'menu_order';
$args['order'] = 'asc';
}
return $args;
}

I was having a problem with a client wanting a specific order while also wanting the sorting to still function. This worked.

@gurudeb
Copy link

gurudeb commented Aug 9, 2015

Thank you!

@knowing8
Copy link

How to make shop page ordering by categories?

@joaquinezcurra
Copy link

Any idea on how to just sort catalog items alphabetically? Is this supposed to be the default behavior?

@SarahMouse
Copy link

Hi there, I have tried adding the code you mentioned above to order by sku:

add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby'); function am_woocommerce_catalog_orderby( $args ) { $args['meta_key'] = '_sku'; $args['orderby'] = 'meta_value'; $args['order'] = 'asc'; return $args; }

But it doesn't work! http://www.playimports.com.au/la-grande-famille/

Any ideas what could be wrong?

Thanks!

@razorfrog
Copy link

Same issue as @silentptnr - Any known solutions for this?

I used the Sort by SKU code and it works. Only problem is numeric sku's are sorting like text instead of numbers. 1,10,100,101 etc. How change I make it so the order is normal numeric. 1,2,3,4,5 etc?

@chekle
Copy link

chekle commented Jul 25, 2016

Thank you!

@pmbaldha
Copy link

There is wordpress woocommerce plugin available which sort featured product first and then make regular sorting. Please see plugin WooCommerce Featured First Wordpress Plugin

@CovinoDaPoet
Copy link

CovinoDaPoet commented Oct 26, 2016

Hi guyz and thank you for the helpful info.

The code works for me, but i have an issue with the order of the products on the product category page.
Product with sku B 3.10 is showing right after product with sku B 3.1 ...is there a solution for this?

just like @razorfrog and @silentptnr

@njwgh
Copy link

njwgh commented Nov 12, 2016

@razorfrog, @silentptnr to sort sku as numeric replace:
$args['orderby'] = 'meta_value';
with
$args['orderby'] = 'meta_value_num';
This gives strict numeric order e.g 10, 90, 101
@CovinoDaPoet - don't know what it will do if you have alpha and numeric in your sku though

@Hoepsi
Copy link

Hoepsi commented Dec 19, 2016

Hey can anyone help with the following:
I used the code to override the default sort order with a custom meta key:
add_filter('woocommerce_get_catalog_ordering_args', 'custom_default_catalog_orderby');

function custom_default_catalog_orderby( $args ) {
if(!isset( $_GET['orderby'] ) ) {
$args['orderby'] = 'meta_value';
$args['order'] = 'asc';
$args['meta_key'] = 'pa_materiaal';

return $args;

}
}

but I would like have the custom meta key sorted on product title as well. So sort first on custom meta key and then sort on product title. Does anyone have suggestions for me?

@johanna75
Copy link

Hi, I need to set the default sorting by price BUT only for one specific category.
Can someone help?

@amjad
Copy link

amjad commented Jun 23, 2017

Is it possible to sort by stock status?

@eagleyed
Copy link

eagleyed commented Sep 1, 2017

Q:- How to sort differently for a particular category e.g. XYZ category? Please help me

add_filter('woocommerce_get_catalog_ordering_args', 'pnp_woocommerce_catalog_orderby');
function pnp_woocommerce_catalog_orderby( $args ) {
	$args['meta_key'] = 'xyzEventsDate';
	$args['orderby'] = date('Y-m-d', strtotime('meta_value_date'));
	$args['order'] = 'DESC';
    return $args;
}

#1 Till now I have now idea how to run the filter function only for a particular category?
#2 Am I doing right in the above code?

@tolkadot
Copy link

tolkadot commented Nov 8, 2017

I would like for one category to be sorted, by default, a different way to the others.
eg - 'new arrivals' default sorted by date - all other categories sorted by popularity.
I have set the default sorting to 'popularity' within the woocommerce settings.

My code below results in the /product-category/new-arrivals page returning no products.

Here is my code to change the sorting order for /product-category/new-arrivals.

`add_filter('woocommerce_get_catalog_ordering_args','tolka_change_ordering',10,1);

function tolka_change_ordering($args)
{
if(is_product_category())
{
global $wp_query;
$cat = $wp_query->get_queried_object();
var_dump($args);

      if($cat->term_id === 68)  //new arrivals
    {
    $args['orderby'] = 'date ID';
	$args['order'] = 'ASC';
	$args['meta_key'] = '';
    }

}
var_dump($args);
return $args;
}
`
Here is the output of the var_dumps:


`array(3) { ["orderby"]=> string(16) "menu_order title" ["order"]=> string(3) "ASC" ["meta_key"]=> string(11) "total_sales" } 
array(3) { ["orderby"]=> string(7) "date ID" ["order"]=> string(3) "ASC" ["meta_key"]=> string(0) "" }`

Any input would be appreciated :)
Dee
ps - not sure what's going on with the formatting - couldn't get the full function in a code block.

@vijaylathiya
Copy link

Thanks
It help me in setup default sorting order for specific category only.

code :
function custom_default_catalog_orderby() {
if( is_product_category('jeans-women')) // jeans-women slug of category for which u want default sort on Date
return 'date'; // Can also use title and price
}

@matt-nelson1
Copy link

None of the code on this page works completely.

Here's a blog post that explains exactly what everyone is trying to do:

https://webhostingbuddy.com/blog/woocommerce-default-sort-method-for-specific-category/

@msjoker
Copy link

msjoker commented Feb 25, 2019

TY :)

@Janilso
Copy link

Janilso commented Jul 25, 2019

Obrigado!

@hoaiphatcr
Copy link

Thank you so much. I try to re-order by modified date and got stuck. Now I see your code. It's save my day.

add_filter('woocommerce_get_catalog_ordering_args', 'am_woocommerce_catalog_orderby');
function am_woocommerce_catalog_orderby( $args ) {
$args['meta_key'] = '';
$args['orderby'] = 'modified';
$args['order'] = 'desc';
return $args;
}

@thientanhai
Copy link

Thanks you i have success in my site

@davITnemec
Copy link

Hi everyone, I'm deal with default sorting with meta key. Using ReHub theme + Woocomerce. I need to have sorted all product regarding their rehub_review_overall_score - it's meta key in ReHub theme.

//Adding custom sort
add_filter( 'woocommerce_get_catalog_ordering_args', 'custom_product_sorting' );
function custom_product_sorting( $args ) {
// Sort score DESC
if ( isset( $_GET['orderby'] ) && 'score-desc' === $_GET['orderby'] ) {
$args['meta_key'] = 'rehub_review_overall_score';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'desc';
}
return $args;
}

//have custom sort as default
add_filter('woocommerce_default_catalog_orderby', 'default_catalog_orderby');
function default_catalog_orderby( $sort_by ) {
return 'score-desc';
}

The code is working properly only when I use it in URL, like ?orderby=score-desc but for default it's not sorting the products regarding their score.

Do you know where is the problem and how to solve it?

Thak you very much

@Hamidaraghi
Copy link

If you want the catalog ordering to still work properly you should wrap your code in something like:

function woocommerce_catalog_orderby( $args ) {
if(!$_GET['orderby']) {
$args['orderby'] = 'menu_order';
$args['order'] = 'asc';
}
return $args;
}

I was having a problem with a client wanting a specific order while also wanting the sorting to still function. This worked.

Thank you sir! 👍

@AngeloLazzari
Copy link

@hoaiphatcr YOU saved my day!!!! thanks!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment