questions

PHP Orderby a number saved as text on database

Rate this post

I’m trying to order a list of users by their mycred balance (integer) but this value is saved as string on database

$args = array(
        'orderby' => 'mycred_default',
        'order'   => 'desc'
);                  

$users = get_users( $args );

How can I convert this string to integer before the query? or can I do it inside the query?

 

✔️Solution:

Probably using meta_value_num could solve the issue

$args = [
    'meta_key' => 'mycred_default',
    'orderby'  => [ 'meta_value_num' => 'DESC' ],
    'order'    => 'desc',
];

$users = get_users( $args );

Leave a Reply

Your email address will not be published.

Back to top button