4

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

2

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' => '='
        )
    )
)); 
4
  • I have something similar to that currently, and I'm using a nested meta query. You cans my current query here: wordpress.stackexchange.com/questions/179274/… It's under the edit in the main question. Problem is that it leads to duplicates being returned so I decided maybe the default search parameter would be better. If I can avoid duplicates I might be able to use meta query. Feb 25, 2015 at 1:23
  • Also, small world :) Feb 25, 2015 at 1:24
  • Duplicate as "same users returned twice"?
    – Sisir
    Jul 24, 2015 at 11:03
  • @Nathan I believe this results in a query where $yoursearchquery is searched in usermeta table among your different meta_key values with an OR relationship, but with an AND relationship when $yoursearchquery is searched in user table among the columns searched (by default or specified by search_columns arg. Sep 6 at 9:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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