2

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

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' => '='
        )
    )
)); 
  • 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. – Thought Space Designs Feb 25 '15 at 1:23
  • Also, small world :) – Thought Space Designs Feb 25 '15 at 1:24
  • Duplicate as "same users returned twice"? – Sisir Jul 24 '15 at 11:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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