Combining Meta_Query key values for one array

I have a situation where my search is working but the client just came with a request I hadn’t considered. If you search by first name (eg. john) or last name (eg. doe), this will absolutely bring your result. But if you search for their full name (eg. john doe), you get 0 results.

I’ve looked around but I can’t seem to find a way to compare the search to “first_name last_name”.

I’ve scanned through the similar questions on the forum but they aren’t helping / seem to go in a different direction entirely. If anyone has any knowledge, or can point me to the right thread, I’d be grateful.

Here’s the summary of my code:

$args = array(
    'role'           => 'Subscriber',
    'meta_query'     => array(
        array(
                'key'     => 'membership_class',
                'value'   => 'Full',
                'compare' => '=',
                'type'    => 'CHAR',
            ),
        array(
            'relation' => 'OR',
            array(
                'key'     => 'first_name',
                'value'   => $usersearch,
                'compare' => 'LIKE'
            ),
            array(
                'key'     => 'last_name',
                'value'   => $usersearch,
                'compare' => 'LIKE'
            ),
            array(
                'key'     => 'personal_city',
                'value'   => $usersearch,
                'compare' => 'LIKE',
                'type'    => 'CHAR',
            ),
            array(
                'key'     => 'personal_province',
                'value'   => $usersearch,
                'compare' => 'LIKE',
                'type'    => 'CHAR',
            ),
            array(
                'key'       => 'treatments',
                'value'     => $usersearch,
                'compare'   => 'LIKE',

            ),



        ),

    ),
);

2 Answers
2

What if you use IN operator and split the search term in a words array:

$args = array(
    'role'           => 'Subscriber',
    'meta_query'     => array(
        array(
                'key'     => 'membership_class',
                'value'   => 'Full',
                'compare' => '=',
                'type'    => 'CHAR',
            ),
        array(
            'relation' => 'OR',
            array(
                'key'     => 'first_name',
                'value'   => explode( ' ', $usersearch ),
                'compare' => 'IN'
            ),
            array(
                'key'     => 'last_name',
                'value'   => explode( ' ', $usersearch ),
                'compare' => 'IN'
            ),
            array(
                'key'     => 'personal_city',
                'value'   => $usersearch,
                'compare' => 'LIKE',
                'type'    => 'CHAR',
            ),
            array(
                'key'     => 'personal_province',
                'value'   => $usersearch,
                'compare' => 'LIKE',
                'type'    => 'CHAR',
            ),
            array(
                'key'       => 'treatments',
                'value'     => $usersearch,
                'compare'   => 'LIKE',

            ),



        ),

    ),
);

Leave a Comment