Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
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

Thanks :)

@mcsolas

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

@Ramoonus

phpDoc?

@mcsolas

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

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!

@Xavien

@niravmehta

Mate, your a saviour. Thank you, I was gona rip woocommerce apart. Thanks again

@mcsolas

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

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

@amiesass

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

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

Hope you can help..

@grimmwerks

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

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

@jhodgski

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

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

@jatinsapra

@ 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

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

@daschina

Hi
and may rotate randomly when updating?

@silentptnr

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?

@max-

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

What if I just wanted featured products to display first?

@sashareds

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

^
using @jatinsapra, update these

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

Cheers!

@peariewang

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

Thanks.

@fricks

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.