I’m using Register Plus Redux plugin to augment a registration form with custom metadata fields. These fields display at the bottom of each user record detail page and they can be retrieved with get_the_author_meta.
In addition, I’m able to create columns in the User panel (the list view) and to make these columns sortable. The problem is that, when I click on the custom column heading, the orderby= value in the URL appears to be ignored. Put another way, it appears the query that generates the list view of users does not include the custom meta data (e.g. perhaps it requires a join statement if the metadata is not in the usual place for user data?). It feels like I’ve missed a step.
Here is my code to create the custom 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);`
And here is my code to make these columns sortable:
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' );`
Any ideas how I can update the query that generates the list of users so that it includes sorting by my custom field(s)? Or, if that’s not the problem, how to make the custom column headings sort the list of users when clicked?
Thank you.
My first answer was completely off-mark and I’m completely rewriting it…
But thanks to Milo’s hint, I found the solution here:
http://wordpress.mcdspot.com/2011/05/24/search-admin-user-list-on-first-and-last-names/
After adapting it, this works for me with ‘facebook’ and ‘twitter’
author_meta
.I’m not sure it’s possible, maybe by modifying the query via
pre_user_query
. here’s a quick and dirty function to view its contents:Simplest way to do it in my opinion:
If your meta values are not fully populated (has nulls), @brasofilo’s excellent answer will produce a user list that omits users with missing meta_values values.
The fix is easy however. Just do a LEFT JOIN:
$user_search->query_from .= " LEFT JOIN {$wpdb->usermeta} m1 ON {$wpdb->users}.ID=m1.user_id AND (m1.meta_key='facebook')";
I was able to achieve user sorting of a custom column using the code and hooks you’ve posted, except I did not use the ‘request’ filter, nor did I need an extra ‘orderby’ function. WordPress properly sorts the data in the custom column in my case by the numerical value that I return in the ‘manage_users_custom_column’ action callback.