Title: MYSQL Query for sorting wordpress usermeta data
Last modified: August 19, 2016

---

# MYSQL Query for sorting wordpress usermeta data

 *  Resolved [estern23](https://wordpress.org/support/users/estern23/)
 * (@estern23)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/mysql-query-for-sorting-wordpress-usermeta-data/)
 * Hi i am trying to do a query against the wordpress usermeta database to get all
   users that have a custom meta_name and value assigned. The custom name/value 
   is a score (integer). I need to bring back the users with the highest score and
   limit the query to 5 results.
 * I have a working statement but it doest want to sort them properly.
 *     ```
       $gather_top_users = "SELECT * FROM ".$wpdb->prefix."usermeta WHERE meta_key='points' ORDER BY meta_value DESC LIMIT 5 ";
       ```
   
 * Any ideas why this is not properly sorting them in the array?

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

 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/mysql-query-for-sorting-wordpress-usermeta-data/#post-1758947)
 * Try
 *     ```
       ORDER BY meta_value+0
       ```
   
 *  Thread Starter [estern23](https://wordpress.org/support/users/estern23/)
 * (@estern23)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/mysql-query-for-sorting-wordpress-usermeta-data/#post-1758956)
 * Thank you, I will test this out later.
 * What is the logic behind adding the “+0” to the order?
 *  [MichaelH](https://wordpress.org/support/users/michaelh/)
 * (@michaelh)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/mysql-query-for-sorting-wordpress-usermeta-data/#post-1758958)
 * Makes it act like a number instead of a string.
 *  Thread Starter [estern23](https://wordpress.org/support/users/estern23/)
 * (@estern23)
 * [15 years, 7 months ago](https://wordpress.org/support/topic/mysql-query-for-sorting-wordpress-usermeta-data/#post-1758959)
 * Awesome, thanks again for your help

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

The topic ‘MYSQL Query for sorting wordpress usermeta data’ is closed to new replies.

## Tags

 * [metadata](https://wordpress.org/support/topic-tag/metadata/)
 * [MySQL](https://wordpress.org/support/topic-tag/mysql/)
 * [query](https://wordpress.org/support/topic-tag/query/)
 * [sorting](https://wordpress.org/support/topic-tag/sorting/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 4 replies
 * 2 participants
 * Last reply from: [estern23](https://wordpress.org/support/users/estern23/)
 * Last activity: [15 years, 7 months ago](https://wordpress.org/support/topic/mysql-query-for-sorting-wordpress-usermeta-data/#post-1758959)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
