开发者

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 resul开发者_运维百科ts.

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?


You might want to cast it to an integer first i.e.

ORDER BY CAST(meta_value AS SIGNED) DESC

See CAST()


Hi @estern:

WordPress' meta_value field is longtext so it won't sort as a number unless you convert it into one so change your ORDER BY from this:

ORDER BY meta_value DESC

To this:

ORDER BY CONVERT(meta_value,UNSIGNED) DESC

Hope this helps.

-Mike

P.S. You might want to check out StackOverflow's sister site WordPress Answers next time you need help with WordPress. Lots of WordPress enthusiasts are on hand over there to answer your WordPress questions.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜