0

I’m trying to add a sortable Country column to the Users screen for one of my BuddyPress xProfile fields. My BuddyPress field is a select2 dropdown, where my members select their country from an array of options.

I have it ~workingish~ (column is displaying on Users screen & sorting by country alphabetically) but it’s throwing a notice:

Notice: Array to string conversion in /srv/www/server/wp-admin/includes/class-wp-users-list-table.php on line 582

& 4 of my 1000+ Users are displaying Array instead of their country.

Here is the code I’ve added to a custom plugin: (from Sortable Custom Columns in User Panel (users.php)?)

// add the custom column
function wpmeo_add_user_columns( $column ) 
{
     $column['Country'] = 'Country';

     return $column;
}
add_filter( 'manage_users_columns', 'wpmeo_add_user_columns' );

// make the custom column sortable
function wpmeo_sortable_user_columns( $columns )
{
    $columns['Country'] = 'country';

    return $columns;
}
add_filter( 'manage_users_sortable_columns', 'wpmeo_sortable_user_columns', 10, 3 );

// provide the custom column value
function wpmeo_user_columns( $value, $column_name, $id ) 
{
    if( 'Country' == $column_name ) 
    {
        $new_column = xprofile_get_field_data( 'Country', $id );
        return $new_column;
    }
}
add_action( 'manage_users_custom_column', 'wpmeo_user_columns', 10, 3 );

// sort by custom column asc & desc
function my_wpmeo_pre_user_query($user_search)
{
    global $wpdb, $current_screen;

    // Only filter in the admin
    if ( ! is_admin() )
        return;

    // Only filter on the users screen
    if ( ! ( isset( $current_screen ) && 'users' == $current_screen->id ) )
        return;

    $vars = $user_search->query_vars;

    if('country' == $vars['orderby'])
    {
        $user_search->query_from .= " INNER JOIN {$wpdb->usermeta} m1 ON {$wpdb->users}.ID=m1.user_id AND (m1.meta_key='country')"; 
        $user_search->query_orderby = ' ORDER BY UPPER(m1.meta_value) '. $vars['order'];
    } 
}
add_action('pre_user_query','my_wpmeo_pre_user_query');

For the last function I've also tried: (from Display sortable User meta column in admin panel user's page)

// sort by custom column asc & desc
function prefix_sort_by_level( $query ) {
    if ( 'country' == $query->get( 'orderby' ) ) {
        $query->set( 'orderby', 'meta_value' );
        $query->set( 'meta_key', 'country' );
    }
}
add_action( 'pre_get_users', 'prefix_sort_by_level' );

& I get the exact same notice & same 4 Users Country display is Array. There is nothing special about these Users or the countries they have selected. I'm assuming that the issue is the type of field (select2 dropdown) but I don't know how to modify either function accordingly.

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.