Skip to main content
user accepted answer and then reverted because WP does not allow sorting by last_name
Source Link
ViralP
  • 310
  • 1
  • 5

3 Steps

  1. Add custom column
  2. Fill data in custom column
  3. Make the column sortable
//Add custom column
function my_custom_wp_filter_add_user_lastname_column($columns) {
    $columns['user_lastname'] = 'Last Name';
    return $columns;
}
add_filter('manage_users_columns', 'my_custom_wp_filter_add_user_lastname_column');

//Fill in value in custom column
function my_custom_wp_filter_add_user_lastname_column_value( $column_output, $column_name, $user_id ) {
    $user = get_userdata( $user_id );
    if ( 'user_lastname' == $column_name ) {
        return $user->last_name;
    }
    return $column_output;
}
add_filter('manage_users_custom_column', 'my_custom_wp_filter_add_user_lastname_column_value', 10, 3);


//Make the column sortable
function my_custom_wp_filter_make_user_lastname_column_sortable( $colums_to_sort ) {
    $colums_to_sort['user_lastname'] = 'last_name';
    return $colums_to_sort;
}
add_filter('manage_users_sortable_columns', 'my_custom_wp_filter_make_user_lastname_column_sortable', 10, 1);

Edit : Response to requirement of ability to sort by user's last name

Wordpress does not allow users to be sorted by last_name by default. See 'orderby' section in WP_User_Query

3 Steps

  1. Add custom column
  2. Fill data in custom column
  3. Make the column sortable
//Add custom column
function my_custom_wp_filter_add_user_lastname_column($columns) {
    $columns['user_lastname'] = 'Last Name';
    return $columns;
}
add_filter('manage_users_columns', 'my_custom_wp_filter_add_user_lastname_column');

//Fill in value in custom column
function my_custom_wp_filter_add_user_lastname_column_value( $column_output, $column_name, $user_id ) {
    $user = get_userdata( $user_id );
    if ( 'user_lastname' == $column_name ) {
        return $user->last_name;
    }
    return $column_output;
}
add_filter('manage_users_custom_column', 'my_custom_wp_filter_add_user_lastname_column_value', 10, 3);


//Make the column sortable
function my_custom_wp_filter_make_user_lastname_column_sortable( $colums_to_sort ) {
    $colums_to_sort['user_lastname'] = 'last_name';
    return $colums_to_sort;
}
add_filter('manage_users_sortable_columns', 'my_custom_wp_filter_make_user_lastname_column_sortable', 10, 1);

3 Steps

  1. Add custom column
  2. Fill data in custom column
  3. Make the column sortable
//Add custom column
function my_custom_wp_filter_add_user_lastname_column($columns) {
    $columns['user_lastname'] = 'Last Name';
    return $columns;
}
add_filter('manage_users_columns', 'my_custom_wp_filter_add_user_lastname_column');

//Fill in value in custom column
function my_custom_wp_filter_add_user_lastname_column_value( $column_output, $column_name, $user_id ) {
    $user = get_userdata( $user_id );
    if ( 'user_lastname' == $column_name ) {
        return $user->last_name;
    }
    return $column_output;
}
add_filter('manage_users_custom_column', 'my_custom_wp_filter_add_user_lastname_column_value', 10, 3);


//Make the column sortable
function my_custom_wp_filter_make_user_lastname_column_sortable( $colums_to_sort ) {
    $colums_to_sort['user_lastname'] = 'last_name';
    return $colums_to_sort;
}
add_filter('manage_users_sortable_columns', 'my_custom_wp_filter_make_user_lastname_column_sortable', 10, 1);

Edit : Response to requirement of ability to sort by user's last name

Wordpress does not allow users to be sorted by last_name by default. See 'orderby' section in WP_User_Query

Source Link
ViralP
  • 310
  • 1
  • 5

3 Steps

  1. Add custom column
  2. Fill data in custom column
  3. Make the column sortable
//Add custom column
function my_custom_wp_filter_add_user_lastname_column($columns) {
    $columns['user_lastname'] = 'Last Name';
    return $columns;
}
add_filter('manage_users_columns', 'my_custom_wp_filter_add_user_lastname_column');

//Fill in value in custom column
function my_custom_wp_filter_add_user_lastname_column_value( $column_output, $column_name, $user_id ) {
    $user = get_userdata( $user_id );
    if ( 'user_lastname' == $column_name ) {
        return $user->last_name;
    }
    return $column_output;
}
add_filter('manage_users_custom_column', 'my_custom_wp_filter_add_user_lastname_column_value', 10, 3);


//Make the column sortable
function my_custom_wp_filter_make_user_lastname_column_sortable( $colums_to_sort ) {
    $colums_to_sort['user_lastname'] = 'last_name';
    return $colums_to_sort;
}
add_filter('manage_users_sortable_columns', 'my_custom_wp_filter_make_user_lastname_column_sortable', 10, 1);