Edit: to clarify, the answer to
How can I merge them together for results?
Is that you really can’t, not without a lot of custom SQL and using filters. The complexity of going that route far outweighs any perceived benefits and wouldn’t be very future-proof. I’d recommend using the solution below and putting it all into a method that you can call to get what you need.
Original Answer
I tested this locally and was able to get two sets of results – from there, I passed them through a callback in array_filter
to remove the duplicate entries:
$q1 = get_users(array(
'fields' => 'all',
'role' => 'contact',
'search' => '*'.esc_attr( $search_term ).'*',
));
$q2 = get_users(array(
'fields' => 'all',
'role' => 'contact',
'meta_query' => array(
'relation' => 'OR',
array(
'key' => 'first_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $search_term,
'compare' => 'LIKE'
),
array(
'key' => 'location',
'value' => $search_term ,
'compare' => 'LIKE'
)
)
));
$results = array_merge( $q1, $q2 );
$results = array_filter( $results, function( $user ) {
static $found_users = [];
if ( in_array( $user->ID, $found_users, true ) ) {
return false;
}
$found_users[] = $user->ID;
return true;
} );
The static
array is persisted over each call to the closure, allowing the code to keep track of which users have already been processed. If a user’s ID is in the $found_users
array, we return false
which removes the current element from the array.