How to use search_columns in WP_User_Query?

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

Read More
$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/

Related posts

Leave a Reply

1 comment

  1. 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');
    }