So I have this code in my function PHP:

Add column

     * Add column to admin > users

function yoursite_manage_users_columns( $columns ) {

    // $columns is a key/value array of column slugs and names
    $columns[ 'custom_field' ] = 'Coins';

    return $columns;

Retrieve data

     * Retrieving data to created column

add_filter( 'manage_users_columns', 'yoursite_manage_users_columns', 10, 1 );

function yoursite_manage_users_custom_column( $output, $column_key, $user_id ) {

    switch ( $column_key ) {
        case 'custom_field':
            $value = get_user_meta( $user_id, 'usercoins', true );

            return $value;
        default: break;

    // if no column slug found, return default output value
    return $output;

add_filter( 'manage_users_custom_column', 'yoursite_manage_users_custom_column', 10, 3 );

Make sortable

 * Make our "Coins" column sortable
add_filter( 'manage_users_sortable_columns', 'yoursite_manage_users_custom_column_sortable' );
function yoursite_manage_users_custom_column_sortable( $columns ) {
    return wp_parse_args( array( 'custom_field' => 'usercoins' ), $columns );

The above code return data (0 to 1000..)

Everything works, except for sorting.

For some reason it sorts by username.

So if I click on sort, it will sort by username alphabet ascending order. If I click again, it will sort by username alphabet descending order.

I need to sort by number count.

Is there something that I'm missing.

Desperately need help.

1 Answer 1


Is there something that I'm missing

Yes, you need to actually add the function which will do the sorting based on your usercoins "sort by" value (that you set in your yoursite_manage_users_custom_column_sortable() function).

So you would hook your function on users_list_table_query_args, then change the args so that the users are being sorted by the usercoins meta value (which is a number, hence I used 'type' => 'NUMERIC'), like so:

add_filter( 'users_list_table_query_args', 'yoursite_users_list_table_query_args' );
function yoursite_users_list_table_query_args( $args ) {
    if ( is_admin() && 'usercoins' === $args['orderby'] ) {
        /* These also work, but to include users who do NOT have the usercoins meta,
         * you should instead use the meta_query arg below:
        $args['meta_key'] = 'usercoins';
        $args['orderby']  = 'meta_value_num';

        $meta_query   = isset( $args['meta_query'] ) ? (array) $args['meta_query'] : [];
        $meta_query[] = array(
            'relation'      => 'OR',
            'has_usercoins' => array(
                'key'  => 'usercoins',
                'type' => 'NUMERIC',
            'no_usercoins'  => array(
                'key'     => 'usercoins',
                'compare' => 'NOT EXISTS',

        $args['meta_query'] = $meta_query;
        $args['orderby']    = 'has_usercoins';

    return $args;

See https://developer.wordpress.org/reference/classes/wp_user_query/prepare_query/ for the full list of arguments you can set or change in the $args array above.

  • 1
    thanks Sally CJ, worked like a charm!
    – robert0
    Jul 26, 2021 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.