According to WP_User_Query only the search s parameter supports a wildcard. Though the meta can accept a comparison such as LIKE instead of =.
But, instead of a wildcard, if you don’t have a value for $searchGender or $searchDiocese don’t add that array to the meta query.
I already did before you mentioned. Thanks by the way!
For any future users, it’d be great if you would share your end code. I might also be able to add it to the FAQ.
// Switch the WP_User_Query args to a meta search
function kia_meta_search( $args ){
// this $_GET is the name field of the custom input in search-author.php
$searchCountries = ( isset($_GET['countries']) ) ? sanitize_text_field($_GET['countries']) : false ;
$searchGender = ( isset($_GET['gender']) ) ? sanitize_text_field($_GET['gender']) : false ;
$searchDiocese = ( isset($_GET['diocese']) ) ? sanitize_text_field($_GET['diocese']) : false ;
$searchCivilStatus = ( isset($_GET['civil_status']) ) ? sanitize_text_field($_GET['civil_status']) : false ;
if ( $searchCountries || $searchGender || $searchDiocese || $searchCivilStatus ){
if ( $searchCountries == '' ) {
$condCountries = 'LIKE';
}else{
$condCountries = '=';
}
if ( $searchGender == '' ) {
$condGender = 'LIKE';
}else{
$condGender = '=';
}
if ( $searchDiocese == '' ) {
$condDiocese = 'LIKE';
}else{
$condDiocese = '=';
}
if ( $searchCivilStatus == '' ) {
$condCivilStatus = 'LIKE';
}else{
$condCivilStatus = '=';
}
$args['meta_query'] = array(
'relation' => 'AND',
array(
'key' => 'billing_country',
'value' => $searchCountries,
'compare' => $condCountries,
),
array(
'key' => 'gender',
'value' => $searchGender,
'compare' => $condGender,
),
array(
'key' => 'diocese',
'value' => $searchDiocese,
'compare' => $condDiocese,
),
array(
'key' => 'civil_status',
'value' => $searchCivilStatus,
'compare' => $condCivilStatus,
)
);
}
return $args;
}
add_filter('sul_user_query_args', 'kia_meta_search');
Does the meta query ignore parts that are set to false? Why don’t you just conditionally add the billing country to the meta query if it exists? And leave it out of the meta query if $searchCountries is false?
$meta_query = array();
if( $searchCountries ){
$meta_query[] = array(
'key' => 'billing_country',
'value' => $searchCountries,
'compare' => '=',
);
}
if( ! empty( $meta_query ) ){
$meta_query['relation'] = 'AND';
$args['meta_query'] = $meta_query
}
Conceptual, and untested, use at your own risk.