Wp_user_query search by meta_key not returning any results

I have a search form, and im trying to have it be able to search both the basic user field (user_email, display_name, etc), plus 1 other custom user meta key called "clinic"

heres the argument i have so far

$args  = array( 
        'orderby'        = 'display_name',                 
        'meta_key'       = 'course',
        'fields'         = 'all',
        'search'         = $search,
        'meta_query' = array(
        array(
            'key' = 'clinic',
            'value' = $search,
            'compare' = 'LIKE'
        )
    )
);

Also, not sure if i have this set up correctly, but i also need the query to check if the meta key "course" exists, regardless of its value (but this is secondary, and can be removed if it might cause problems, im mostly concerned with it searching the clinic meta key)

Topic wp-user-query Wordpress

Category Web


You're setting two different meta_key fields to check on: 'course', and 'clinic'. Try doing it with just one like so:

$args = array(
    'orderby' => 'display_name',
    'fields'  => 'all',
    'search'  => $search,
    'meta_query' => array(
            array(
                'key'     => 'clinic',
                'value'   => $search,
                'compare' => 'LIKE'
            )
    )
 );

What your current query is doing is returning users who match the $search parameter on the basic user columns AND the meta key "clinic". Is this your intention?

Update 3/24/16:

Try this:

$args = array(
    'orderby'    => 'display_name',
    'fields'     => 'all',    
    'search'     => $search,
    'meta_query' => array(
        'relation' => 'OR',
            array(
                'key'     => 'course',
                'value'   => $search,
                'compare' => 'LIKE'
            ),
            array(
                'key'     => 'clinic',
                'value'   => $search,
                'compare' => 'LIKE'
            )
    )
);

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.