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
}

Thanks :)

mcsolas commented Jan 24, 2012

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

phpDoc?

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.

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 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 commented Apr 5, 2012

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

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!!

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

Hope you can help..

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.

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

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 commented Mar 18, 2013

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

@ 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 commented Aug 2, 2013

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

Hi
and may rotate randomly when updating?

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?

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 commented Nov 4, 2013

What if I just wanted featured products to display first?

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?

^
using @jatinsapra, update these

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

Cheers!

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

Thanks.

Thanks.

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 commented Aug 9, 2015

Thank you!

How to make shop page ordering by categories?

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

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!

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 commented Jul 25, 2016

Thank you!

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 commented Oct 26, 2016 edited

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

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

amjad commented Jun 23, 2017

Is it possible to sort by stock status?

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