Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am using WP_User_Query to search use by name, ID, email etc... In that case I need to use search_columns field to pass ID or email of user.

What i am doing is

$my_users = new WP_User_Query( 
                    array( 
                        'role' => $role,
                        'offset' => $offset ,
                        'number' => $number,
                        'fields' => 'all',
                        'search_columns'=> array( '5','', 'johndoe_dd@yahoo1.com','',''),
                        //'search_columns'=> $search_columns_array,
                        'meta_query' => $meta_query_array,
                    ));

For reference http://wpsmith.net/2012/wp/an-introduction-to-wp_user_query-class/

share|improve this question
add comment

1 Answer

up vote 2 down vote accepted

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');
}
share|improve this answer
    
thanks bro!, this is the one that i am searching for. –  Ajay Patel Oct 17 '12 at 12:42
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.