I'm building an advanced User search using WP_User_Query but experiencing some unusual behaviour. I'm trying to search with a term that I know exists in the user_nicename and user_email fields but fails to return when I include meta_query in the $args.
Using the simple query below, when searching the users table data, WordPress returns everything as expected because the term exists in the user_nicename and user_email.
$args = array (
'fields' => ['ID'],
'count_total' => true,
'order' => 'ASC',
'orderby' => 'display_name',
'search' => '*'.esc_attr( $search_term ).'*',
);
$wp_user_query = new WP_User_Query($args);
Then however, when I introduce a meta_query into the search to check against the user meta, the query fails to return any results at all - even though it's matching data previously using the simple statement. The meta_query makes the $args look like so:
$args = array (
'fields' => ['ID'],
'count_total' => true,
'order' => 'ASC',
'orderby' => 'display_name',
'search' => '*'.esc_attr( $search_term ).'*',
'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' => 'description',
'value' => $search_term ,
'compare' => 'LIKE'
)
)
);
$wp_user_query = new WP_User_Query($args);
I'm determining which columns to search from the users table by implementing the following filter:
function inc_columns( $search_columns, $search, $this ) {
$search_columns[] = 'user_email';
$search_columns[] = 'user_nicename';
$search_columns[] = 'display_name';
return $search_columns;
}
add_filter('user_search_columns', 'inc_columns', 10, 3);
Any light that can be shed on why this is would be great.