Per the documentation for WP_User_Query, there is an available search parameter. It looks something like this:

$users = new WP_User_Query(array(
    'search' => 'This is my search'
));

You can even set the columns in the database that are searched against using the “search_columns” parameter like so:

$users = new WP_User_Query(array(
    'search' => 'This is my search',
    'search_columns' => array( 'user_login', 'user_email' )
));

Now, the documentation mentions that you can modify the different columns available for search using the user_search_columns filter. That filter is defined on this page:

Filter Reference/user search columns

I don’t see anything about searching based on usermeta though. I want to add usermeta as a search column. Does anybody know how to do this?

1 Answer
1

Try this:

$yoursearchquery = 'This is my search';
$users = new WP_User_Query(array(
    'search' => $yoursearchquery,
    'meta_query' => array(
        'relation' => 'OR',
        array(
            'key' => 'shoe_size',
            'value' => $yoursearchquery,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'shoe_color',
            'value' => $search_operation,
            'compare' => 'LIKE'
        ),
        array(
            'key' => 'shoe_maker',
            'value' => $yoursearchquery,
            'compare' => '='
        )
    )
)); 

Leave a Reply

Your email address will not be published. Required fields are marked *