Viewing 8 replies - 1 through 8 (of 8 total)
  • You need to specify that the meta_value is an integer and not a string

    I believe what your looking for is:

    meta_value_num

    So:

    $users = get_users(array(
        'role' => 'subscriber',
        'orderby'    => 'meta_value_num',
        'meta_key' => 'badge_number',
        'order'      => 'ASC'
    ));

    From the codex:

    'meta_value_num' - Order by numeric meta value (available with Version 2.8). Also note that a 'meta_key=keyname' must also be present in the query. This value allows for numerical sorting as noted above in 'meta_value'.

    Thread Starter spencermerpi

    (@spencermerpi)

    Evan, how do I do that? I tried meta_value_num and it doesn’t work

    Thread Starter spencermerpi

    (@spencermerpi)

    I think meta_value_num works when you’re querying posts but not when you are querying users

    It looks like you need to specify a meta_query within get_users array:

    maybe something like:

    $users = get_users(array(
        'role' => 'subscriber',
        'meta_key' => 'badge_number',
        'orderby'    => 'meta_value_num',
        'meta_query'   => array(
          'key' => 'badge_number',
        ),
        'order'      => 'ASC'
    ));

    Thread Starter spencermerpi

    (@spencermerpi)

    Evan, Thank you for continuing to help. Unfortunately that also does not work

    hmm, I know it’s possible – it just takes a bit of tinkering.

    I know you need a meta_query in there

    Thread Starter spencermerpi

    (@spencermerpi)

    I’ve tried everything I can think of and no success

    Thread Starter spencermerpi

    (@spencermerpi)

    So this is not the best way, but my numbers will never go above the hundreds place, so I made each number a 5 decimal number. 1 now equals 00001 number 301 now equals 00301. Then I use $str = ltrim($str, ‘0’); to remove the leading zeros so the user can’t see them.

    Like I said, not the best way, but it got the job done

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘Ordering User Query by Numeric Custom Field’ is closed to new replies.