Wordpress $wpdb->usermeta sort by meta_value
I am trying to display all users based on a meta_key called "points" like this:
$wpdb->get_col($wpdb->prepare("
SELECT user_id, meta_key = 'points', meta_value
FROM $wpdb->usermeta
ORDER BY meta_value DESC
LIMIT $limit OFFSET {$paginate->offset()}"));
The users are displayed properly, but the order is not working, meta_value is equal to a number from 1 to ∞. So, how should I get it to work? Thanks.
PS: This is the output:
Array ( [0] => 1 [1] => 开发者_开发问答2 [2] => 4 )
So I believe is ordered by ID.
SELECT user_id, meta_value FROM $wpdb->usermeta WHERE meta_key = 'points' ORDER BY CAST(meta_value AS SIGNED) DESC LIMIT
CAST(meta_value AS SIGNED) will covert from LONGTEXT to INT. It works now.
If I'm not completely wrong about mysql, you have to add the meta_key='points' to the where clause. Rewrite the query to:
SELECT user_id, meta_value
FROM $wpdb->usermeta
WHERE meta_key = 'points'
ORDER BY meta_value DESC
LIMIT $limit OFFSET {$paginate->offset()}
精彩评论