我已将在我的网站上注册的用户的登录级别定义为级别1和级别2。我在管理面板用户页面(users.php)中添加了一列,我试图添加排序选项,但单击排序不起作用。元键是level
. 这是我试过的。
add_filter( \'manage_users_columns\', \'myplugin_manage_users_columns\');
function myplugin_manage_users_columns( $columns )
{
$columns[\'login_level\'] = \'Login Level\';
return $columns;
}
add_filter(\'manage_users_custom_column\', \'myplugin_manage_users_custom_column\', 10, 3);
function myplugin_manage_users_custom_column( $value, $column_name, $user_id )
{
$user = get_userdata( $user_id );
if (\'login_level\' == $column_name){
$lLevel = get_user_meta( $user_id, \'level\', true );
$value = $lLevel;
return $value;
}
}
//make the new column sortable
function user_sortable_columns( $columns ) {
$columns[\'login_level\'] = \'login_level\';
return $columns;
}
add_filter( \'manage_users_sortable_columns\', \'user_sortable_columns\' );
//set instructions on how to sort the new column
function my_user_query($userquery){
if(\'login_level\'==$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 = \'level\' ";//which meta are we sorting with?
$userquery->query_orderby = " ORDER BY alias.meta_value ".($userquery->query_vars["title"] == "ASC" ? "asc " : "desc ");//set sort order
}
}
最合适的回答,由SO网友:Akshat 整理而成
我替换了
//make the new column sortable
function user_sortable_columns( $columns ) {
$columns[\'login_level\'] = \'login_level\';
return $columns;
}
add_filter( \'manage_users_sortable_columns\', \'user_sortable_columns\' );
//set instructions on how to sort the new column
function my_user_query($userquery){
if(\'login_level\' == $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 = \'login_level\' ";//which meta are we sorting with?
$userquery->query_orderby = " ORDER BY alias.meta_value ".($userquery->query_vars["title"] == "ASC" ? "asc " : "desc ");//set sort order
}
}
具有
//make the new column sortable
function prefix_sortable_columns( $columns ) {
$columns[\'login_level\'] = \'login_level\';
return $columns;
}
add_filter( \'manage_users_sortable_columns\', \'prefix_sortable_columns\' );
function prefix_sort_by_level( $query ) {
if ( \'login_level\' == $query->get( \'orderby\' ) ) {
$query->set( \'orderby\', \'meta_value\' );
$query->set( \'meta_key\', \'level\' );
}
}
add_action( \'pre_get_users\', \'prefix_sort_by_level\' );
一切都结束了。