get_col($wpdb-...">

Wordpress $ wpdb-> usermeta sort by meta_value

I am trying to display all meta_key based users called "dots" 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()}"));

Users are displayed correctly, but the order does not work, meta_value is equal to a number from 1 to ∞. So how do I get it to work? Thank.

PS: This is the conclusion:

Array ( [0] => 1 [1] => 2 [2] => 4 ) 

So, I believe, sorted by ID.

+3
source share
2 answers
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 be hidden from LONGTEXT to INT. Now it works.

+6
source

If I'm not completely wrong in mysql, you need to add meta_key = 'points' to the where clause. Rewrite the request to:

SELECT user_id, meta_value
FROM $wpdb- > usermeta
WHERE meta_key = 'points'
ORDER BY meta_value DESC
LIMIT $limit OFFSET {$ paginate- > offset()}

+1

Source: https://habr.com/ru/post/1776446/


All Articles