7

I am in the process of adding an additional column to the Users admin screen to display the company for each user. I have been successful in getting this column to show in the table, but I am really struggling with making it sortable alphabetically.

The column header does seem to be activated as sortable, but if i click it to reorganise the list order, the table actually rearranges alphabetically based on the username as opposed to the company.

I have spent alot of time on the web looking for and adapting other solutions, but still no luck. I have seen plenty of examples of making custom columns sortable for post type admins screens but not the user admin screen.

Below is the code I am currently using to generate a "Company" column on the users admin screen and it is pulling in the author meta data "company".

//MAKE THE COLUMN SORTABLE

function user_sortable_columns( $columns ) {
    $columns['company'] = 'Company';
    return $columns;
}

add_filter( 'manage_users_sortable_columns', 'user_sortable_columns' );

add_action( "pre_get_users", function ( $WP_User_Query ) {

if ( isset( $WP_User_Query->query_vars["orderby"] )
    && ( "company" === $WP_User_Query->query_vars["orderby"] )
) {
    $WP_User_Query->query_vars["meta_key"] = "company_name";
    $WP_User_Query->query_vars["orderby"] = "meta_value";
}

}, 10, 1 );
  • Possible duplicate of Sortable Custom Columns in User Panel (users.php)? – Andy Macaulay-Brook Aug 4 '16 at 15:44
  • Thanks Andy, but I had already come across that example and could not get it to work! – Jordan Rogers Aug 4 '16 at 16:24
  • The author of this answer wordpress.stackexchange.com/a/52349/94267 seems to think it worked OK in their tests. If you've tried this and it doesn't work it would help if you updated your question to show this. You don't currently have anything in your question showing the SQL JOINS that are in that answer, for example. – Andy Macaulay-Brook Aug 4 '16 at 16:27
  • I feel like you're not prepending this as a new meta-query. & instead overriding any existing meta-queries that are in place. – admcfajn Jul 1 '18 at 0:53
4

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.

  • Where did you set any ordering functionality for your column? It's supposed to be a default functionality? I'm trying to order by "Role", not working :) – trainoasis Mar 13 at 21:51
  • I think that's the fc_my_sortable_cake_column() function, coupled with the add_filter( 'manage_users_sortable_columns', 'fc_my_sortable_cake_column' ); call. I would try that code, replacing $columns['vendor_id'] = 'Vendor ID'; with $columns['role'] = 'Role';. – ban-geoengineering Mar 14 at 18:20
  • 1
    ...I've just tried it (by adding $columns['role'] = 'Role'; to my fc_my_sortable_cake_column function). It works. :-) – ban-geoengineering Mar 14 at 18:22
1

the action request works with post. for user it's pre_get_users :

add_action("pre_get_users", function ($WP_User_Query) {

    if (    isset($WP_User_Query->query_vars["orderby"])
        &&  ("company" === $WP_User_Query->query_vars["orderby"])
    ) {
        $WP_User_Query->query_vars["meta_key"] = "company_name";
        $WP_User_Query->query_vars["orderby"] = "meta_value";
    }

}, 10, 1);
  • Hello. Thanks for you help with this. I was wandering if you could please take a look at how i have used your suggested code, as I still cant seem to get this working properly. if I click to sort on the 'Company' column, it still seems to actually sorted alphabetically based on the 'Username' column. I have updated my original question with your code included where I believe it is meant to go. – Jordan Rogers Aug 4 '16 at 14:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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