The default relation is AND
so you really don’t need to specify that, but I’ll show you how to do it anyway.
Really, you just have an array manipulation problem.
$amenities = array('a','b','c');
$amenities2 = array('d','e','f');
foreach ($amenities as $a)
{
$arrays[] = array(
'key' => 'amenities',
'value' => $a,
'compare' => 'LIKE'
);
}
foreach ($amenities2 as $a2)
{
$arrays2[] = array(
'key' => 'amenities',
'value' => $a2,
'compare' => 'like'
);
}
$meta_query = array('relation' => 'AND');
$meta_query[] = $arrays;
$meta_query[] = $arrays2;
var_dump($meta_query);
I also cleaned up your code. It was overly complicated and broken. The if
statements were overkill for iterating over an array and the $count = count($arrays);
lines were unnecessary and didn’t do anything as you were count
ing the arrays before they were created. Even if it worked, you had the possibility of clobbering one value with another. If the counts were equal you’d end up overwriting part of the array, not adding to it.
Of course, that is not the only way to put together the final array. In fact, that is probably not the way I’d do it if it were my code. That is probably the easiest way to demonstrate things though.