How to use search_columns in WP_User_Query?

This is covered in the link you reference: The search_columns attribute does not set what should be searched for in each column, but instead specifies which columns should be searched for the term set in ‘search’.

Specifically, you can only search for one term – but you can look in one or more columns.

//Search user logins & emails for 'foo'.
$my_users = new WP_User_Query( 
              array( 
                'fields' => 'all',
                'search'=>'foo',
                'search_columns'=> array('user_login', 'user_email'),
            ));

From the source, the default values are:

  • user_email – if an ‘@’ is present in the search term
  • user_login, ID – if not, but the term is numeric
  • user_url – if not, but the term looks like an url (and you have less than 10000 users).
  • Failing to meet any of the above, it defaults to searching user_login and user_nicename

The source:

$search_columns = array();
if ( $qv['search_columns'] )
    $search_columns = array_intersect( $qv['search_columns'], array( 'ID', 'user_login', 'user_email', 'user_url', 'user_nicename' ) );
if ( ! $search_columns ) {
    if ( false !== strpos( $search, '@') )
        $search_columns = array('user_email');
    elseif ( is_numeric($search) )
        $search_columns = array('user_login', 'ID');
    elseif ( preg_match('|^https?://|', $search) && ! wp_is_large_network( 'users' ) )
        $search_columns = array('user_url');
    else
        $search_columns = array('user_login', 'user_nicename');
}