2

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',

            ),



        ),

    ),
);
  • Thanks for cleaning up that left alignment there Howdy, I posted this in a hurry last night – Faye Jul 12 '17 at 18:15
1

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',

            ),



        ),

    ),
);
  • That works!!!! I'm going to mark this as the answer over my own, because it's less code. Thanks so much! – Faye Jul 12 '17 at 18:25
0

I hunkered down and solved this today. Hoping my success will help anyone else in the same boat.

Because the meta query array NEEDS a true meta_key, you actually need to create one for the user and populate it with the information you want to pass. See the add_user_meta function for more info.

So, prior to running my search, I have this:

<?php 
$allusers = get_users(array(
    'role' => 'subscriber',

));

foreach ($allusers as $user) {
    $firstName = get_user_meta($user->ID, 'first_name', true);
    $lastName = get_user_meta($user->ID, 'last_name', true);

    $full_name = $firstName . ' ' . $lastName . ' ';

    $uid = $user->ID;


    add_user_meta( $uid, 'full_name', $full_name );


}
?>

Here I've called all the users, and then I've defined for them all new meta user, with my custom value that includes both the first and last name of the user (ignore my use of custom variables over standard ones, I have a lot of user queries going on this page and just wanted to make sure I wasn't causing any conflicts).

Then down in my search query I've added one more query...

array(
    'key'     => 'full_name',
    'value'   => $usersearch,
    'compare' => 'LIKE'
),

And that has my search functioning for all three, John, Doe, and John Doe.

  • i went with the other answer provided by dbeja, but leaving this here for anyone that truly needs a custom meta. – Faye Jul 12 '17 at 18:27

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.