How to make a custom column on the Users admin screen sortable?

This is my code which adds a sortable custom column (called Vendor ID) to the users table:

function fc_new_modify_user_table( $column ) {
    $column['vendor_id'] = 'Vendor ID';
    return $column;
}
add_filter( 'manage_users_columns', 'fc_new_modify_user_table' );

function fc_new_modify_user_table_row( $val, $column_name, $user_id ) {
    switch ($column_name) {
        case 'vendor_id' :
            return get_the_author_meta( 'vendor_id', $user_id );
        default:
    }
    return $val;
}
add_filter( 'manage_users_custom_column', 'fc_new_modify_user_table_row', 10, 3 );

function fc_my_sortable_cake_column( $columns ) {
    $columns['vendor_id'] = 'Vendor ID';

    //To make a column 'un-sortable' remove it from the array unset($columns['date']);

    return $columns;
}
add_filter( 'manage_users_sortable_columns', 'fc_my_sortable_cake_column' );

Simple and works fine for me.

Leave a Comment