Title: Custom Sort By
Last modified: February 4, 2019

---

# Custom Sort By

 *  Resolved [jasonlpeck](https://wordpress.org/support/users/jasonlpeck/)
 * (@jasonlpeck)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/custom-sort-by/)
 * I’m trying to add a custom sort by option to the drop down when viewing products.
   I’ve added the following code, but when the option is selected, I’m routed to
   [https://new.tmvavra.com/search/?orderby=test](https://new.tmvavra.com/search/?orderby=test)
   and I get a No Products Found message. I suspect I’m not using the correct meta-
   key value but can’t seem to get it to work. What am I missing?
 * function custom_product_sort( $sort_args ) {
    // Get the selected order by value
   $orderby_value = isset( $_GET[‘orderby’] ) ? woocommerce_clean( $_GET[‘orderby’]):
   apply_filters( ‘woocommerce_default_catalog_orderby’, get_option( ‘woocommerce_default_catalog_orderby’));
 *  switch ($orderby_value) {
    case ‘alphabetical’ : $sort_args[‘order’] = ‘ASC’;
   $sort_args[‘meta_key’] = ”; $sort_args[‘orderby’] = ‘title’; break; case ‘test’:
   $sort_args[‘post_type’] = ‘product’; $sort_args[‘order’] = ‘ASC’; **$sort_args[‘
   meta_key’] = ‘bathrooms’;** $sort_args[‘orderby’] = ‘meta_value_num’; break; }
 *  return $sort_args;
    } add_filter( ‘woocommerce_get_catalog_ordering_args’, ‘
   custom_product_sort’ );
 * function add_custom_product_sort_options( $sortby ) {
    $sortby[‘alphabetical’]
   = ‘Sort by name’; $sortby[‘test’] = __( ‘Sort by Test’, ‘textdomain’ ); return
   $sortby; } add_filter( ‘woocommerce_default_catalog_orderby_options’, ‘add_custom_product_sort_options’);
   add_filter( ‘woocommerce_catalog_orderby’, ‘add_custom_product_sort_options’ );
 * The page I need help with: _[[log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fwordpress.org%2Fsupport%2Ftopic%2Fcustom-sort-by%2F%3Foutput_format%3Dmd&locale=en_US)
   to see the link]_

Viewing 2 replies - 1 through 2 (of 2 total)

 *  [Peter Lawrenson](https://wordpress.org/support/users/lorro/)
 * (@lorro)
 * [7 years, 3 months ago](https://wordpress.org/support/topic/custom-sort-by/#post-11168940)
 * “bathrooms” looks like a category, not a meta key. Its not possible to sort by
   category, since products may be in multiple categories.
 * To find available meta keys, go to phpMyAdmin and look at the postmeta table.
   Pick a product ID.
 * Ones you might use include:
    _price _regular_price _sale_price _weight _length
   _width _height _sku _stock _download_limit
 *  [Luminus Alabi](https://wordpress.org/support/users/luminus/)
 * (@luminus)
 * Automattic Happiness Engineer
 * [7 years, 3 months ago](https://wordpress.org/support/topic/custom-sort-by/#post-11197130)
 * Hi [@jasonlpeck](https://wordpress.org/support/users/jasonlpeck/),
 * We haven’t heard back from you in a while, so I’m going to mark this as resolved–
   if you have any further questions, you can start a new thread.
 * Thanks, [@lorro](https://wordpress.org/support/users/lorro/) for the assist.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Custom Sort By’ is closed to new replies.

 * ![](https://ps.w.org/woocommerce/assets/icon.svg?rev=3234504)
 * [WooCommerce](https://wordpress.org/plugins/woocommerce/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/woocommerce/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/woocommerce/)
 * [Active Topics](https://wordpress.org/support/plugin/woocommerce/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/woocommerce/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/woocommerce/reviews/)

 * 2 replies
 * 3 participants
 * Last reply from: [Luminus Alabi](https://wordpress.org/support/users/luminus/)
 * Last activity: [7 years, 3 months ago](https://wordpress.org/support/topic/custom-sort-by/#post-11197130)
 * Status: resolved