admin管理员组文章数量:1289543
So I have this code in my function PHP:
Add column
/*
* Add column to admin > users
*/
function yoursite_manage_users_columns( $columns ) {
// $columns is a key/value array of column slugs and names
$columns[ 'custom_field' ] = 'Coins';
return $columns;
}
Retrieve data
/*
* Retrieving data to created column
*/
add_filter( 'manage_users_columns', 'yoursite_manage_users_columns', 10, 1 );
function yoursite_manage_users_custom_column( $output, $column_key, $user_id ) {
switch ( $column_key ) {
case 'custom_field':
$value = get_user_meta( $user_id, 'usercoins', true );
return $value;
break;
default: break;
}
// if no column slug found, return default output value
return $output;
}
add_filter( 'manage_users_custom_column', 'yoursite_manage_users_custom_column', 10, 3 );
Make sortable
/*
* Make our "Coins" column sortable
*/
add_filter( 'manage_users_sortable_columns', 'yoursite_manage_users_custom_column_sortable' );
function yoursite_manage_users_custom_column_sortable( $columns ) {
return wp_parse_args( array( 'custom_field' => 'usercoins' ), $columns );
}
The above code return data (0 to 1000..)
Everything works, except for sorting.
For some reason it sorts by username.
So if I click on sort, it will sort by username alphabet ascending order. If I click again, it will sort by username alphabet descending order.
I need to sort by number count.
Is there something that I'm missing.
Desperately need help.
So I have this code in my function PHP:
Add column
/*
* Add column to admin > users
*/
function yoursite_manage_users_columns( $columns ) {
// $columns is a key/value array of column slugs and names
$columns[ 'custom_field' ] = 'Coins';
return $columns;
}
Retrieve data
/*
* Retrieving data to created column
*/
add_filter( 'manage_users_columns', 'yoursite_manage_users_columns', 10, 1 );
function yoursite_manage_users_custom_column( $output, $column_key, $user_id ) {
switch ( $column_key ) {
case 'custom_field':
$value = get_user_meta( $user_id, 'usercoins', true );
return $value;
break;
default: break;
}
// if no column slug found, return default output value
return $output;
}
add_filter( 'manage_users_custom_column', 'yoursite_manage_users_custom_column', 10, 3 );
Make sortable
/*
* Make our "Coins" column sortable
*/
add_filter( 'manage_users_sortable_columns', 'yoursite_manage_users_custom_column_sortable' );
function yoursite_manage_users_custom_column_sortable( $columns ) {
return wp_parse_args( array( 'custom_field' => 'usercoins' ), $columns );
}
The above code return data (0 to 1000..)
Everything works, except for sorting.
For some reason it sorts by username.
So if I click on sort, it will sort by username alphabet ascending order. If I click again, it will sort by username alphabet descending order.
I need to sort by number count.
Is there something that I'm missing.
Desperately need help.
Share Improve this question asked Jul 26, 2021 at 8:51 robert0robert0 2032 silver badges11 bronze badges1 Answer
Reset to default 1Is there something that I'm missing
Yes, you need to actually add the function which will do the sorting based on your usercoins
"sort by" value (that you set in your yoursite_manage_users_custom_column_sortable()
function).
So you would hook your function on users_list_table_query_args
, then change the args so that the users are being sorted by the usercoins
meta value (which is a number, hence I used 'type' => 'NUMERIC'
), like so:
add_filter( 'users_list_table_query_args', 'yoursite_users_list_table_query_args' );
function yoursite_users_list_table_query_args( $args ) {
if ( is_admin() && 'usercoins' === $args['orderby'] ) {
/* These also work, but to include users who do NOT have the usercoins meta,
* you should instead use the meta_query arg below:
$args['meta_key'] = 'usercoins';
$args['orderby'] = 'meta_value_num';
*/
$meta_query = isset( $args['meta_query'] ) ? (array) $args['meta_query'] : [];
$meta_query[] = array(
'relation' => 'OR',
'has_usercoins' => array(
'key' => 'usercoins',
'type' => 'NUMERIC',
),
'no_usercoins' => array(
'key' => 'usercoins',
'compare' => 'NOT EXISTS',
),
);
$args['meta_query'] = $meta_query;
$args['orderby'] = 'has_usercoins';
}
return $args;
}
See https://developer.wordpress/reference/classes/wp_user_query/prepare_query/ for the full list of arguments you can set or change in the $args
array above.
本文标签: functionsSortable column (by numbers) in admin users
版权声明:本文标题:functions - Sortable column (by numbers) in admin users 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741420854a2377795.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论