I run a blog site with posts by authors, and I implemented a search that would return results if the search was like an author's name or the search term was contained in a story. I was functioning and properly returned authors who matched the search, but now the search returns every user every single time.
I suspect it has something to do with WP_User_query since
since that function was updated in 4.2, but I have no idea how I would change it to be compatible.
Here is the search code:
add_filter('user_search_columns', 'user_search_columns_bd' , 10, 3);
function user_search_columns_bd($search_columns, $search, $this){
if(!in_array('display_name', $search_columns)){
$search_columns[] = 'display_name';
}
return $search_columns;
}
add_filter( 'get_meta_sql', 'user_meta_filter', 10, 6 );
function user_meta_filter( $sql, $queries, $type, $primary_table, $primary_id_column, $context ){
if ( $type !== 'user' ){
return $sql;
}
if ( ! isset( $context->query_vars['meta_query']['replace_and'] ) || $context->query_vars['meta_query']['replace_and'] !== true ){
return $sql;
}
$sql['where'] = preg_replace('/AND/', 'OR', $sql['where'], 1);
return $sql;
}
$args = array(
'search' => $s,
'search_columns' => array( 'user_login', 'user_email'),
'meta_query' => array(
'relation' => 'OR',
'replace_and' => true,
array(
'key' => 'first_name',
'value' => $s,
'compare' => 'LIKE'
),
array(
'key' => 'last_name',
'value' => $s,
'compare' => 'LIKE'
)
)
);
// The Query
$user_query = new WP_User_Query( $args );