1

I have added some custom code to my functions.php to add column sorting for several of my custom meta fields. I have encountering two issues. 1. For some reason when I add this code all of the data for the roles column (subscriber, administrator, etc.) dissapears. When I delete this code the roles data is back. I'm not sure why. And 2. When I sort a column, let's say city, it sorts fine, but if a user has a blank entry for city, that user isn't displayed. I still want the the user with a blank field to be displayed when sorting. Thanks!

add_filter('manage_users_columns', 'add_user_columns', 15, 1);
function add_user_columns($columns) {
$columns['city'] =__('City','myplugindomain');
$columns['state'] =__('State','myplugindomain');
return $columns;
}

//Print the user data in the new column
function add_custom_user_columns($value, $column_name, $id) {
  if( $column_name == 'city' ) {
    return get_the_author_meta( 'city', $id );
    }
      if( $column_name == 'state' ) {
        return get_the_author_meta( 'state', $id );
      }
}
add_action('manage_users_custom_column', 'add_custom_user_columns', 15, 3);


//Add sortable columns
function prefix_sortable_columns( $columns ) {
$columns['city'] = 'city';
$columns['state'] = 'state';
return $columns;

}
add_filter( 'manage_users_sortable_columns', 'prefix_sortable_columns' );


//Sort
function prefix_sort_by_expiration_date( $query ) {
if ( 'city' == $query->get( 'orderby' ) ) {
$query->set( 'orderby', 'meta_value' );
    $query->set( 'meta_key', 'city' );
}
if ( 'state' == $query->get( 'orderby' ) ) {
$query->set( 'orderby', 'meta_value' );
    $query->set( 'meta_key', 'state' );
}
}
add_action( 'pre_get_users', 'prefix_sort_by_expiration_date' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.