Giselle Boyer
Giselle Boyer

Reputation: 13

PHP Orderby a number saved as text on database

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?

Upvotes: 1

Views: 43

Answers (1)

Andrii Kovalenko
Andrii Kovalenko

Reputation: 2227

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 );

Upvotes: 1

Related Questions