Quantcast
Channel: WordPress › Support » Forum: Hacks - Recent Topics
Viewing all articles
Browse latest Browse all 8245

scdwb on "Custom users.php column to Order by user_meta"

$
0
0

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 all articles
Browse latest Browse all 8245

Trending Articles