I have a text field and a drop down with country names. If I use OR and only fill in the text field with a person's name it brings back incorrect results. If I type in the name and select a country, I get correct results. If I input a name and purposefully chose the incorrect country that they are not in, I get results for the country I selected. So, something is not working correctly here.
If I use AND and only type in the persons name and leave out the country, I get no results where I would like to at least see some results based on the name search. Neither fields are required, you should be able to only search by 1 or both.
If you search only by location it should show all users for that country and if you only input a name it should show all users with that name in any country and if you search with both fields it should only show you people with that name in the specified country.
$s_query = $_GET['s'];
$country = $_GET['location'];
$args = array(
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $s_query,
'compare' => 'LIKE'
) ,
array(
'key' => 'last_name',
'value' => $s_query,
'compare' => 'LIKE'
),
array(
'key' => 'location',
'value' => $country,
'compare' => 'LIKE'
)
)
);
$wp_user_query = new WP_User_Query($args);
$authors = $wp_user_query->get_results();
if (!empty($authors)) {
echo '<ul>';
foreach ($authors as $author) {
$author_info = get_userdata($author->ID);
echo '<li>' . $author_info->first_name . ' ' . $author_info->last_name . ' - ' . $author->location . '</li>';
}
echo '</ul>';
} else {
echo 'No authors found';
}