wp user query - Wp_user_query search by meta_key not returning any results

admin2025-01-08  3

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)

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)

Share Improve this question asked Mar 23, 2016 at 21:30 blank473blank473 291 silver badge6 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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'
            )
    )
);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736268402a1272.html

最新回复(0)