0

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)

0

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'
            )
    )
);
  • didnt work for me, its not returning any results. at least before it was returning results for the basic one (email, and display name), but the one you posted didnt work for any of them. – blank473 Mar 24 '16 at 13:16
  • Shouldn't this be 'compare' => 'EXISTS' for the course field? According to the question, the value is irrelevant. – swissspidy Feb 4 '18 at 20:02

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.