• Resolved scdwb

    (@scdwb)


    Hello,

    I’ve added a new sortable column to the Users.php page in the WordPress admin (Users > All Users).

    This works really well and the new column is populated by a custom user_meta value. Here is the code:

    // Register payment_pending_count sortable column
    add_filter('manage_users_columns', 'add_payment_pending_column');
    function add_payment_pending_column($columns) {
        $columns['payment_pending_count'] = 'Payment Pending Count';
        return $columns;
    }
    
    add_filter( 'manage_users_sortable_columns', 'add_payment_pending_column_sortable' );
    function add_payment_pending_column_sortable( $columns ){
    	$columns['payment_pending_count'] = 'payment_pending_count';
    	return $columns;
    }
    
    add_action('manage_users_custom_column',  'show_payment_pending_column_content', 10, 3);
    function show_payment_pending_column_content($value, $column_name, $user_id) {
    if ( 'payment_pending_count' == $column_name )
    return get_user_meta($user_id, 'payment_pending_count', true);
    return $value;
    }

    This displays a nice new “sortable” column and I can click the column. However, the problem is finding a solution on how to tell WordPress to sort by this custom user_meta value. The following function doesn’t work and WordPress still sorts by the default setting of the “username” when I try and sort this new custom column:

    function payment_pending_column_orderby( $vars ) {
        if ( isset( $vars['orderby'] ) && 'payment_pending_count' == $vars['orderby'] ) {
            $vars = array_merge( $vars, array(
                'meta_key' => 'payment_pending_count',
                'meta_type'    => 'numeric',
            ) );
        }
        return $vars;
    }
    add_filter( 'request', 'payment_pending_column_orderby' );

    I think I’m nearly there, I just need to be able to sort by this custom user meta. By default, the WordPress users.php page already lets us sort by the user’s email, name and username. And these are all user_meta values. I’ve tried searching in the core code of WordPress to see if I can replicate how it’s been done with these values but I’ve been unable to find the right code. I may just need pointing in the right direction.

    Many thanks,
    Steven

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter scdwb

    (@scdwb)

    I’ve just solved this using a different method of telling WordPress how to sort the column. Instead of hooking into the “request” filter in my function, I’m using this function and hooking into the “pre_user_query” action:

    add_action('pre_user_query', 'status_column_orderby');
    function status_column_orderby($userquery){
    if('payment_pending_count'==$userquery->query_vars['orderby']) {
    global $wpdb;
    $userquery->query_from .= " LEFT OUTER JOIN $wpdb->usermeta AS alias ON ($wpdb->users.ID = alias.user_id) ";//note use of alias
    $userquery->query_where .= " AND alias.meta_key = 'payment_pending_count' ";//which meta are we sorting with?
    $userquery->query_orderby = " ORDER BY alias.meta_value ".($userquery->query_vars["order"] == "ASC" ? "asc " : "desc ");//set sort order
    }
    }

    Not as pretty as what I was trying before. I was trying to avoid calling the database in this way but it works. FYI, my custom user_meta name is ‘payment_pending_count’.

    Hope this helps others.

    Steven

    Hey Steven,

    Great post. Was searching around everywhere for this. The use of the pre_user_query was mentioned but never code examples.

    I’ve gotten this to work but there’s a slight glitch. That being that if there is no data in the meta field, that record falls out of the table when you sort it (i.e., certain records disappear). Have you seen this and, if so, do you have any ideas on how to ensure all records are returned?

    Hey Steven,

    Did you figure out how to sort the meta value numerically?

    I’m having trouble with “99” always coming before something like “673”. The wpdb datatype of meta_value is longtext.

    Thanks if you can help, Ron

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Custom users.php column to Order by user_meta’ is closed to new replies.