Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
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
}
@mustardBees

This comment has been minimized.

Show comment Hide comment
@mustardBees

mustardBees Jan 17, 2012

Thanks :)

Thanks :)

@mcsolas

This comment has been minimized.

Show comment Hide comment
@mcsolas

mcsolas Jan 24, 2012

Thank you!! I was getting frustrated trying to do this.

mcsolas commented Jan 24, 2012

Thank you!! I was getting frustrated trying to do this.

@Ramoonus

This comment has been minimized.

Show comment Hide comment
@Ramoonus

Ramoonus Feb 11, 2012

phpDoc?

phpDoc?

@mcsolas

This comment has been minimized.

Show comment Hide comment
@mcsolas

mcsolas Feb 11, 2012

strangely, the site has reverted to using ABC order, even with this code in place. maybe something changed when I moved to the most current version (from 1.3 to 1.4). it was working. this code made both the front page and any tags show the way I wanted. Both now have reverted with 0 code changes made within the child theme I am using based on the twenty-eleven theme.

mcsolas commented Feb 11, 2012

strangely, the site has reverted to using ABC order, even with this code in place. maybe something changed when I moved to the most current version (from 1.3 to 1.4). it was working. this code made both the front page and any tags show the way I wanted. Both now have reverted with 0 code changes made within the child theme I am using based on the twenty-eleven theme.

@niravmehta

This comment has been minimized.

Show comment Hide comment
@niravmehta

niravmehta Mar 19, 2012

Like @mcsolas mentioned, the given code did not work.

But this worked:

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

Note that this code sorts by price - highest to lowest. Doing it on date is simpler. Skip the 'meta_key', and set 'orderby' as 'date'.

HTH!

Like @mcsolas mentioned, the given code did not work.

But this worked:

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

Note that this code sorts by price - highest to lowest. Doing it on date is simpler. Skip the 'meta_key', and set 'orderby' as 'date'.

HTH!

@mcsolas

This comment has been minimized.

Show comment Hide comment
@mcsolas

mcsolas Mar 21, 2012

when I upgraded from 1.3 to 1.4 I had already applied the function in my theme and it was already showing most recent first on my site. on upgrade, the site "cached" a the old default ABC value, somehow didn't seem to care about my theme override and started showing the site in ABC order again. Eventually support in their forum suggested that it was a browser issue and to clear the cookies / history. It worked, the site started showing the most recent product first again.

mcsolas commented Mar 21, 2012

when I upgraded from 1.3 to 1.4 I had already applied the function in my theme and it was already showing most recent first on my site. on upgrade, the site "cached" a the old default ABC value, somehow didn't seem to care about my theme override and started showing the site in ABC order again. Eventually support in their forum suggested that it was a browser issue and to clear the cookies / history. It worked, the site started showing the most recent product first again.

@jperocho

This comment has been minimized.

Show comment Hide comment
@jperocho

jperocho Apr 5, 2012

is there a way to sort it via sku? or maybe sort by custom meta box?

jperocho commented Apr 5, 2012

is there a way to sort it via sku? or maybe sort by custom meta box?

@amiesass

This comment has been minimized.

Show comment Hide comment
@amiesass

amiesass Sep 20, 2012

I liked the way this code defaulted the sorting to price highest to lowest, but it also makes it so that the sort alphabetically or by most recent not work... I just want the default to be sort by highest to lowest price, but still have the other two options available that work.. any ideas? Thanks!!

I liked the way this code defaulted the sorting to price highest to lowest, but it also makes it so that the sort alphabetically or by most recent not work... I just want the default to be sort by highest to lowest price, but still have the other two options available that work.. any ideas? Thanks!!

@jimcallender

This comment has been minimized.

Show comment Hide comment
@jimcallender

jimcallender Nov 27, 2012

Is it possible for this function to 'show all' products?

Hope you can help..

Is it possible for this function to 'show all' products?

Hope you can help..

@grimmwerks

This comment has been minimized.

Show comment Hide comment
@grimmwerks

grimmwerks Dec 17, 2012

I've been going nuts trying to find the actual filter woocommerce_catalog_orderby -- where is it?

I too want to be able to sort by whether something is in stock or not - ie adding additional sort functionality -- but not sure how best to go about it.

I've been going nuts trying to find the actual filter woocommerce_catalog_orderby -- where is it?

I too want to be able to sort by whether something is in stock or not - ie adding additional sort functionality -- but not sure how best to go about it.

@jhodgski

This comment has been minimized.

Show comment Hide comment
@jhodgski

jhodgski Feb 21, 2013

grimmwerks, the woocommerce_catalog_orderby filter is located in woocommerce/templates/loop/sorting.php .

grimmwerks, the woocommerce_catalog_orderby filter is located in woocommerce/templates/loop/sorting.php .

@jhodgski

This comment has been minimized.

Show comment Hide comment
@jhodgski

jhodgski Feb 21, 2013

mikejolley's initial code worked fine for me. Incidentally, his function can also return another value which I have used so that the 'order' attribute of my products are used to sort the products...

function custom_default_catalog_orderby() {
return 'menu_order'; // Can also use title, date and price
}

mikejolley's initial code worked fine for me. Incidentally, his function can also return another value which I have used so that the 'order' attribute of my products are used to sort the products...

function custom_default_catalog_orderby() {
return 'menu_order'; // Can also use title, date and price
}

@aj-adl

This comment has been minimized.

Show comment Hide comment
@aj-adl

aj-adl Mar 18, 2013

@jhodgski the option 'default' returns 'menu_order' for the orderby argument anyway, no need to update it with this function.

aj-adl commented Mar 18, 2013

@jhodgski the option 'default' returns 'menu_order' for the orderby argument anyway, no need to update it with this function.

@jatinsapra

This comment has been minimized.

Show comment Hide comment
@jatinsapra

jatinsapra Mar 26, 2013

@ jperocho use this code to Sort 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;
}

@ jperocho use this code to Sort 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;
}
@pwkip

This comment has been minimized.

Show comment Hide comment
@pwkip

pwkip Aug 2, 2013

How would you go about changing the default sorting for a single category?

pwkip commented Aug 2, 2013

How would you go about changing the default sorting for a single category?

@daschina

This comment has been minimized.

Show comment Hide comment
@daschina

daschina Aug 15, 2013

Hi
and may rotate randomly when updating?

Hi
and may rotate randomly when updating?

@silentptnr

This comment has been minimized.

Show comment Hide comment
@silentptnr

silentptnr Sep 29, 2013

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?

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?

@ameliapower

This comment has been minimized.

Show comment Hide comment
@ameliapower

ameliapower Sep 29, 2013

niravmehta and jatinsapra's code both work, thank you. However the other sorting criteria that comes with woocommerce plugin in templates/loop/orderby.php like title, price etc will no longer work when I add this meta sorting to functions file.

I need to be able to sort by the various options provided with the plugin as well as several custom fields that I have added to my products. How can I integrate these meta sorting options with what is already in place?

niravmehta and jatinsapra's code both work, thank you. However the other sorting criteria that comes with woocommerce plugin in templates/loop/orderby.php like title, price etc will no longer work when I add this meta sorting to functions file.

I need to be able to sort by the various options provided with the plugin as well as several custom fields that I have added to my products. How can I integrate these meta sorting options with what is already in place?

@ckpicker

This comment has been minimized.

Show comment Hide comment
@ckpicker

ckpicker Nov 4, 2013

What if I just wanted featured products to display first?

ckpicker commented Nov 4, 2013

What if I just wanted featured products to display first?

@sashareds

This comment has been minimized.

Show comment Hide comment
@sashareds

sashareds Jan 17, 2014

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?

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

This comment has been minimized.

Show comment Hide comment
@cipriantepes

cipriantepes Jan 29, 2014

^
using @jatinsapra, update these

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

Cheers!

^
using @jatinsapra, update these

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

Cheers!

@peariewang

This comment has been minimized.

Show comment Hide comment
@peariewang

peariewang Aug 15, 2014

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;
}

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

This comment has been minimized.

Show comment Hide comment
@limsocheat

limsocheat Feb 23, 2015

Thanks.

Thanks.

@jpcmf

This comment has been minimized.

Show comment Hide comment
@jpcmf

jpcmf Jun 30, 2015

Thanks.

jpcmf commented Jun 30, 2015

Thanks.

@nickzamboli

This comment has been minimized.

Show comment Hide comment
@nickzamboli

nickzamboli Aug 7, 2015

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.

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

This comment has been minimized.

Show comment Hide comment
@gurudeb

gurudeb Aug 9, 2015

Thank you!

gurudeb commented Aug 9, 2015

Thank you!

@knowing8

This comment has been minimized.

Show comment Hide comment
@knowing8

knowing8 Nov 17, 2015

How to make shop page ordering by categories?

How to make shop page ordering by categories?

@bongotai

This comment has been minimized.

Show comment Hide comment
@bongotai

bongotai Nov 18, 2015

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

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

@SarahMouse

This comment has been minimized.

Show comment Hide comment
@SarahMouse

SarahMouse Jul 15, 2016

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!

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

This comment has been minimized.

Show comment Hide comment
@razorfrog

razorfrog Jul 22, 2016

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?

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

This comment has been minimized.

Show comment Hide comment
@chekle

chekle Jul 25, 2016

Thank you!

chekle commented Jul 25, 2016

Thank you!

@pmbaldha

This comment has been minimized.

Show comment Hide comment
@pmbaldha

pmbaldha Sep 14, 2016

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

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

This comment has been minimized.

Show comment Hide comment
@CovinoDaPoet

CovinoDaPoet 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

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

This comment has been minimized.

Show comment Hide comment
@njwgh

njwgh 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

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

This comment has been minimized.

Show comment Hide comment
@Hoepsi

Hoepsi 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?

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

This comment has been minimized.

Show comment Hide comment
@johanna75

johanna75 Mar 16, 2017

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

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

@amjad

This comment has been minimized.

Show comment Hide comment
@amjad

amjad Jun 23, 2017

Is it possible to sort by stock status?

amjad commented Jun 23, 2017

Is it possible to sort by stock status?

@icraftzone

This comment has been minimized.

Show comment Hide comment
@icraftzone

icraftzone 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?

icraftzone 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

This comment has been minimized.

Show comment Hide comment
@tolkadot

tolkadot 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.

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

This comment has been minimized.

Show comment Hide comment
@vijaylathiya

vijaylathiya Jan 3, 2018

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
}

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
}

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