i want to create filter with meta fields i create meta fields with ACF plugin, i want to use or relation for some meta_field but they dont work,
for example on filter i choose "Male" and "Female" for meta key trainer this show me no posts but one post have meta value as Male but my query dont show them, but if i select only "Male" this query is show post
meta values in post is array('Male')
This is for ajax filter on wordpress
$args = [
'posts_per_page' => - 1,
];
if ( $minlength ) {
$min_lenght_query[] = [
'key' => 'duration',
'value' => $minlength,
'compare' => '>=',
'type' => 'NUMERIC'
];
}
if ( $maxlength ) {
$max_lenght_query[] = [
'key' => 'duration',
'value' => $maxlength,
'compare' => '<=',
'type' => 'NUMERIC'
];
}
if ( $minburn ) {
$minburn_query[] = [
'key' => 'calorie_burn_minimum',
'value' => $minburn,
'compare' => '>=',
'type' => 'NUMERIC'
];
}
if ( $maxburn ) {
$maxburn_query[] = [
'key' => 'calorie_burn_maximum',
'value' => $maxburn,
'compare' => '<=',
'type' => 'NUMERIC'
];
}
if ( $difficulty ) {
$difficulty_query[] = [
'key' => 'difficulty',
'value' => $difficulty,
'compare' => 'IN',
];
}
if ( $trainer ) {
foreach ($trainer as $value) {
$trainer_query[] = [
'key' => 'trainer',
'value' => $value,
'compare' => 'LIKE',
];
}
}
if ( $focus ) {
foreach ($focus as $value) {
$focus_query[] = [
'key' => 'body_focus',
'value' => $value,
'compare' => 'LIKE',
];
}
}
if ( $trainingtype ) {
foreach ($trainingtype as $value) {
$trainingtype_query[] = [
'key' => 'training_type',
'value' => $value,
'compare' => 'LIKE',
];
}
}
if ( $equipment ) {
foreach ($trainingtype as $value) {
$equipment_query[] = [
'key' => 'equipment',
'value' => $value,
'compare' => 'LIKE',
];
}
}
if ( $search ) {
$args['s'] = $search;
}
if($sort == 'newest'){
$args['orderby'] = [ 'date'=>'DESC' ];
}elseif($sort == 'olders'){
$args['orderby'] = [ 'date'=>'ASC' ];
}elseif($sort == 'shortest'){
$args['orderby'] = [ 'meta_value_num'=>'ASC' ];
$args['meta_key'] = 'duration';
}elseif($sort == 'longest'){
$args['orderby'] = [ 'meta_value_num'=>'DESC' ];
$args['meta_key'] = 'duration';
}
$args['meta_query']=array(
'relation' => 'AND',
array($min_lenght_query),
array($max_lenght_query),
array($minburn_query),
array($maxburn_query),
array($difficulty_query),
array('relation' => 'OR',$trainer_query),
array('relation' => 'OR',$focus_query),
array('relation' => 'OR',$trainingtype_query),
array('relation' => 'OR',$equipment_query),
);
I found how i can do this after testing the arrays
if ( $trainer ) {
$trainer_query=array('relation' => 'OR',);
foreach ($trainer as $value) {
$trainer_query[] = [
'key' => 'trainer',
'value' => $value,
'compare' => 'LIKE',
];
}
}
and use this like that in meta query and this solve my problem
$args['meta_query']=array(
'relation' => 'AND',
array($min_lenght_query),
array($max_lenght_query),
array($minburn_query),
array($maxburn_query),
array($difficulty_query),
array('relation' => 'OR',$trainer_query),
array('relation' => 'OR',$focus_query),
array('relation' => 'OR',$trainingtype_query),
array('relation' => 'OR',$equipment_query),
);