• Resolved xxaimsxx

    (@xxaimsxx)


    Hello, I tried to sort the role column on Users admin page but it is not working. It seems to be sorting based on username. Is it because there are UM roles and default WP roles to select from when creating users?

    I have tried this code but it’s not working:

    add_filter( 'manage_users_sortable_columns', 'register_sortable_columns_custom', 10, 1 );
    function register_sortable_columns_custom( $columns ) {
      $columns['role'] = 'role';
      return $columns;
    }
    • This topic was modified 2 years, 1 month ago by xxaimsxx.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Support yuriinalivaiko

    (@yuriinalivaiko)

    Hello @xxaimsxx ,

    You can not use this solution to make the Role column sortable because the 'role' usermeta doesn’t exist. WordPress stores user’s roles in the 'wp_capabilities' usermeta. The value of this usermeta is an array because users may have multiple roles. So, this usermeta can not be used for sorting.

    You need a workaround to sort users by their primary role:

    • add a code that writes a primary user role to the custom usermeta as a simple string when the user’s roles are changed.
    • add a column that displays the custom usermeta with the primary user role to the Users table.
    • make this custom column sortable.

    Just an advice: Try to use role filtering instead of role sorting. You can use links above the table to display users who have a certain role.

    Regards

    Plugin Support yuriinalivaiko

    (@yuriinalivaiko)

    Hi xxaimsxx,

    This thread has been inactive for a while so we’re going to go ahead and mark it Resolved.

    Please feel free to re-open this thread if any other questions come up and we’d be happy to help. ??

    Regards

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Sort role column on Users admin page’ is closed to new replies.