Skip to content
Create a gist now

Instantly share code, notes, and snippets.

WooCommerce - Custom sorting options (asc/desc)
* This code should be added to functions.php of your theme
add_filter('woocommerce_get_catalog_ordering_args', 'custom_woocommerce_get_catalog_ordering_args');
function custom_woocommerce_get_catalog_ordering_args( $args ) {
if (isset($_SESSION['orderby'])) {
switch ($_SESSION['orderby']) :
case 'date_asc' :
$args['orderby'] = 'date';
$args['order'] = 'asc';
$args['meta_key'] = '';
case 'price_desc' :
$args['orderby'] = 'meta_value_num';
$args['order'] = 'desc';
$args['meta_key'] = '_price';
case 'title_desc' :
$args['orderby'] = 'title';
$args['order'] = 'desc';
$args['meta_key'] = '';
return $args;
add_filter('woocommerce_catalog_orderby', 'custom_woocommerce_catalog_orderby');
function custom_woocommerce_catalog_orderby( $sortby ) {
$sortby['title_desc'] = 'Reverse-Alphabetically';
$sortby['price_desc'] = 'Price (highest to lowest)';
$sortby['date_asc'] = 'Oldest to newest';
return $sortby;

Can you sort by custom sort options that include meta_key, meta_value, and meta_compare. Essentially I am trying to only show products with a specific meta value such as:

case 'title_bulk' :
$args['orderby'] = 'meta_value';
$args['order'] = 'asc';
$args['meta_key'] = 'product_packaging';
$args['meta_value'] = 'Bulk';
$args['meta_compare'] = '=';

jurajk commented May 28, 2013

replace $_SESSION with $_GET and magically, totally custom sorting starts working. found this at

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.