I have an array like so:
$args = array(
'post_type' => 'player',
'posts_per_page' => -1,
);
I then have another array, such as:
[stateArray] => Array
(
[0] => OR
[1] => WI
)
Which I need to add to the original $args array to specifically get this result:
$args = array(
'post_type' => 'player',
'posts_per_page' => -1,
'meta_query' => array(
'compare' => 'AND',
[0] => array(
'key' => 'state',
'value' => 'OR',
'compare' => '=',
),
[1] => array(
'key' => 'state',
'value' => 'WI',
'compare' => '=',
),
)
);
Trying this code:
$stateArray = $_POST['stateArray'];
$state_array_wrapper = array('meta_query' => array());
foreach ($stateArray as $state) {
$single_state_array = array(
'key' => 'town_state',
'value' => $state,
'compare' => '='
);
array_push($state_array_wrapper, $single_state_array);
}
$state_array_wrapper = array_values($state_array_wrapper);
array_push($args, $state_array_wrapper);
I get this result:
Array (
[post_type] => player
[posts_per_page] => -1
[0] => Array
(
[0] => Array
(
)
[1] => Array
(
[key] => town_state
[value] => OR
[compare] => =
)
[2] => Array
(
[key] => town_state
[value] => WI
[compare] => =
)
)
)
Not sure why I don't see 'meta_query' in the final array there at all, or how to just push those two $single_state_arrays into meta_query even if I did see it showing up. How can this be done properly?