Title: Order by meta value
Last modified: August 21, 2016

---

# Order by meta value

 *  Resolved [njbebop](https://wordpress.org/support/users/njbebop/)
 * (@njbebop)
 * [12 years, 4 months ago](https://wordpress.org/support/topic/order-by-meta-value/)
 * Hi Everyone,
 * I’m currently running a WP 3.8.1 install with WP E-Commerce plugin. The powers
   that be want to be able to sort their products to move out of stock items to 
   the back of the line.
 * In the plugin, the stock is in the postmeta table under key “_wpsc_stock”. What
   I am trying to do is order the products by the meta_value of that key (either
   1 or 0).
 * I have the filter in my functions.php, and it updates the existing query with
   whatever I put in.
 * add_filter( ‘posts_orderby’, ‘products_orderby’ );
 * function products_orderby( $orderby )
    { global $wpdb; $orderby = $wpdb->postmeta.‘.
   meta_value DESC, ‘ . $orderby; return $orderby; }
 * I know this is incorrect, I need to know what I can put into the function to 
   order this by the meta_value. I’ve been looking everywhere, seen every variation,
   and nothing seems to work.
 * Thanks!

Viewing 1 replies (of 1 total)

 *  Thread Starter [njbebop](https://wordpress.org/support/users/njbebop/)
 * (@njbebop)
 * [12 years, 3 months ago](https://wordpress.org/support/topic/order-by-meta-value/#post-4730561)
 * Issue resolved.

Viewing 1 replies (of 1 total)

The topic ‘Order by meta value’ is closed to new replies.

## Tags

 * [functions](https://wordpress.org/support/topic-tag/functions/)
 * [order](https://wordpress.org/support/topic-tag/order/)
 * [orderby](https://wordpress.org/support/topic-tag/orderby/)
 * [query](https://wordpress.org/support/topic-tag/query/)

 * 1 reply
 * 1 participant
 * Last reply from: [njbebop](https://wordpress.org/support/users/njbebop/)
 * Last activity: [12 years, 3 months ago](https://wordpress.org/support/topic/order-by-meta-value/#post-4730561)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
