WordPress.org

Ready to get started?Download WordPress

Forums

make extra columns in user.php sortable (17 posts)

  1. ensonic
    Member
    Posted 2 years ago #

    I am trying to make extra columns added by http://wordpress.org/extend/plugins/check-last-login/
    sortable. Just add the code below to the end of the current plugin code. Something happens to the sort-order, but its not sorting the strings.

    add_filter( 'manage_users_sortable_columns', 'cll_users_edit_columns' );
    
    // this does not fully work yet :/
    function custom_column_orderby( $vars ) {
    	if ( isset( $vars['orderby'] )) {
                    if ('Registered' == $vars['orderby'] ) {
    		        $vars = array_merge( $vars, array(
    			        'meta_key' => 'Registered',
    			        'orderby' => 'meta_value_num'
    		        ) );
                    } elseif ('Lastlogin' == $vars['orderby'] ) {
    		        $vars = array_merge( $vars, array(
    			        'meta_key' => 'Lastlogin',
    			        'orderby' => 'meta_value_num'
    		        ) );
                    }
    	}
    
    	return $vars;
    }
    add_filter( 'request', 'custom_column_orderby' );
  2. ensonic
    Member
    Posted 2 years ago #

    I learned that I should use 'orderby' => 'meta_value' as my columns contain strings. Unfortunately it still does not work as expected.

  3. ensonic
    Member
    Posted 2 years ago #

    I made a little more progress. The values for orderby are not related to the screen table, but to the database. Thus for "Registered" I have to use "user_registered". This won't work for "Lastlogin" as this is stored as user_meta. Any idea?

  4. fredhead
    Member
    Posted 2 years ago #

    If you're still struggling with this issue, you might want to check out this article:

    http://wordpress.stackexchange.com/questions/20639/sortable-columns-on-a-custom-post-type-wont-work

    I'm using a registration plugin that creates meta-data (e.g. company name, title) and was able to hack code from the article and get sortable columns on my users.php page:

    function user_sortable_columns( $columns ) {
    	$columns['company'] = 'Company';
    	return $columns;
    }
    add_filter( 'manage_users_sortable_columns', 'user_sortable_columns' );
    
    function user_column_orderby( $vars ) {
    	if ( isset( $vars['orderby'] ) && 'company' == $vars['orderby'] ) {
    		$vars = array_merge( $vars, array(
    			'meta_key' => 'company',
    			'orderby' => 'meta_value',
    			'order'     => 'asc'
    		) );
    	}
    	return $vars;
    }
    add_filter( 'request', 'user_column_orderby' );

    While it doesn't appear thoroughly documented anywhere, apparently there is a manage_users_sortable_columns filter. Indeed, the "users" part apparently could be post or a custom content type name.

    The only issue I had was matching the column names I defined for each of my user custom columns for user.php and, once figured out, the code worked immediately, on any custom column. If it is useful, here is that code (which is placed above the code that orders these columns):

    //add columns to User panel list page
    function add_user_columns( $defaults ) {
         $defaults['company'] = __('Company', 'user-column');
         $defaults['title'] = __('Title', 'user-column');
         return $defaults;
    }
    function add_custom_user_columns($value, $column_name, $id) {
          if( $column_name == 'company' ) {
    		return get_the_author_meta( 'company_name', $id );
          }
          if( $column_name == 'title' ) {
    		return get_the_author_meta( 'titlefunction', $id );
          }
    }
    add_action('manage_users_custom_column', 'add_custom_user_columns', 15, 3);
    add_filter('manage_users_columns', 'add_user_columns', 15, 1);

    The name of the author_meta value is set in the registration plugin I'm using.

    Hope that helps!

  5. ensonic
    Member
    Posted 2 years ago #

    Still stuck with the same issue. I still wonder what actually gets sorted. Is WP calling add_custom_user_columns() for each column and sorting the returned values? Or is it doing a join in the database and sorting the meta-values as they are in the db?

  6. fredhead
    Member
    Posted 2 years ago #

    I believe the issue is updating the query so that it sorts by the meta-value(s). I'm also still stuck, despite my post above (which is close to the solution needed). The people on Stack Exchange probably have an answer but were unavailable when I posted a month ago.

    Here's the info on WP_User_Query that needs to be updated to include the meta-value to be sorted on:

    http://codex.wordpress.org/Class_Reference/WP_User_Query

    To make things even more confusing, the Stack Exchange link above uses WP_User_Search which is deprecated, as of 3.1, for WP_User_Query. And the actions you want, I believe, might be called pre_user_search and pre_user_query. They work but they're not documented anywhere I could find.

    Bottomline, I gave up. It was easier to add Company to the search results than to make the column sortable.

  7. Tallyopia
    Member
    Posted 2 years ago #

    FWIW, I'm also stuck here. I didn't have much trouble adding the fields, but I still can't get them to sort. Something happens to the order when I click on the headings, but I can't tell what it is.

    If anyone has any updates, please post them.

  8. sync3ds
    Member
    Posted 2 years ago #

    I've the same problem and i've noticed that the 'request' filter isn't applied for the users' list! It's only called for posts and pages.

    Still looking for solutions.

  9. steve.jin
    Member
    Posted 2 years ago #

    I've the same problem , and using

    add_action('pre_user_query', 'my_user_query');
    function my_user_query($userquery){
    
    	echo "<div style='margin-top:30px;' class='update-nag'>";
       print_r($userquery);
       echo '</div>';
    }
    to out put the action code, the custom colum not working and it's sort by user_login , the result as
    WP_User_Query Object ( [results] => [total_users] => 0 [query_fields] => SQL_CALC_FOUND_ROWS wp_users.ID [query_from] => FROM wp_users [query_where] => WHERE 1=1 [query_orderby] => ORDER BY user_login ASC [query_limit] => LIMIT 10 [query_vars] => Array ( [blog_id] => 1 [role] => [meta_key] => [meta_value] => [meta_compare] => [include] => Array ( ) [exclude] => Array ( ) [search] => [orderby] => huodong [order] => ASC [offset] => 0 [number] => 10 [count_total] => 1 [fields] => all_with_meta [who] => ) )

    If anyone has any updates, please post them.

  10. circlecube
    Member
    Posted 1 year ago #

    I'm having the same issues. I'm trying to sort a custom field for users. Adding a column for 'company' works great:

    [Code moderated as per the Forum Rules. The maximum number of lines of code that you can post in these forums is ten lines. Please use the pastebin]

    and entering the code to sort the column appears to load, but then the sorting is not according to the correct column, it sorts it just the same as if I were sorting by username.

    [Code moderated]

    I guess as stated above the user page does not call the register function and therefore the filter is not called either? Is this just because it hasn't been set up to work on the users page or is it by design?

  11. circlecube
    Member
    Posted 1 year ago #

    Looks like we need to use the 'pre_user_query' action. Any ideas on how? http://core.trac.wordpress.org/ticket/17573

  12. President McCheese
    Member
    Posted 1 year ago #

    Extending the WP_List_Table class for your plugins would probably offer greater flexibility and control.

  13. rcmck
    Member
    Posted 1 year ago #

    Simple class to add sortable user meta columns to wp-admin/users.php table. (You can also add wp_users columns).

    /**
     * Add columns to the wp-admin user's table
     *
     * @param array $args Associative array where key is wp_usermeta meta_keys or wp_users column names and the value is the display name
     * @return none;
     */
    if(!function_exists(load_sortable_user_meta_columns)){
    	add_action('admin_init', 'load_sortable_user_meta_columns');
    	function load_sortable_user_meta_columns(){
    		//THIS IS WHERE YOU ADD THE meta_key => display-title values
    		$args = array('nickname'=>'Nickname', 'user_registered'=>'Date Registered');
    		new sortable_user_meta_columns($args);
    	}
    }
    if(!class_exists(sortable_user_meta_columns)):
    class sortable_user_meta_columns{
    	var $defaults = array('nicename', 'email', 'url', 'registered','user_nicename', 'user_email', 'user_url', 'user_registered','display_name','name','post_count','ID','id','user_login');
    	function __construct($args){
    		$this->args = $args;
    		add_action('pre_user_query', array(&$this, 'query'));
    		add_action('manage_users_custom_column',  array(&$this, 'content'), 10, 3);
    		add_filter('manage_users_columns', array(&$this, 'columns'));
    		add_filter( 'manage_users_sortable_columns', array(&$this, 'sortable') );
    	}
    	function query($query){
    		$vars = $query->query_vars;
    		if(in_array($vars['orderby'], $this->defaults)) return;
    		$title = $this->args[$vars['orderby']];
    		if(!empty($title)){
    			   $query->query_from .= " LEFT JOIN wp_usermeta m ON (wp_users.ID = m.user_id  AND m.meta_key = '$vars[orderby]')";
    			   $query->query_orderby = "ORDER BY m.meta_value ".$vars['order'];
    		}
    	}
    	function columns($columns) {
    		foreach($this->args as $key=>$value){
    			$columns[$key] = $value;
    		}
    		return $columns;
    	}
    	function sortable($columns){
    		foreach($this->args as $key=>$value){
    			$columns[$key] = $key;
    		}
    		return $columns;
    	}
    	function content($value, $column_name, $user_id) {
    		$user = get_userdata( $user_id );
    		return $user->$column_name;
    	}
    }
    endif;
  14. justmigrating
    Member
    Posted 1 year ago #

    rcmck - I would like to give you credit for your code, which I used for a client. Can you give me your name or your website url?

  15. justmigrating
    Member
    Posted 1 year ago #

    actually, the plugin didn't work! lol. it simply overrode the custom fields created from another plugin. oh, well.

  16. bluantinoo
    Member
    Posted 1 year ago #

    actually the plugin work but not perfectly. I can add sortable columns using user_meta datas.

    but I get some errors at the top of users list and when trying to save a user in backend

    Notice: Use of undefined constant load_sortable_user_meta_columns - assumed 'load_sortable_user_meta_columns'

    Notice: Use of undefined constant sortable_user_meta_columns - assumed 'sortable_user_meta_columns'

    Notice: Undefined index: login in

  17. bluantinoo
    Member
    Posted 1 year ago #

    People, after a quite a lot of searching I must thanks uncle Google for showing up this result: Codepress Admin Columns

    it's just amazing, works perfectly: you can add wichever columns, not only to usersbut even to media library, comments, links and wathever post type!

    the only downside is that to have all the column sortable you need to buy the "sortable addon", but actually I think this plugin it's worth 15 bukcs!

    and I'm using it in collaboration with User Meta Pro Plugin.

    The Pro version of User Meta is still a young product and not much support is provided, but in my opinion still worth 16 bucks.

    so, for a total user management I suggest to use them together.
    Two small prices for user management troubles liberation ;)

    works like a charm!

Topic Closed

This topic has been closed to new replies.

About this Topic