0

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?

2
  • The example you show as "what I need" is impossible. You can't have elements in an array that don't have key values. You inserted two arrays into an array, but neither one has a key. The answers below give them key values 0 and 1. Commented Nov 22, 2019 at 19:50
  • Sure @kainaw, you're correct. I update the question. I am not familiar with why, when writing code like this you don't have to write the [0] => parts but the chosen answer below worked for me, keys and all. Commented Nov 22, 2019 at 20:08

2 Answers 2

1

You need to give your $args the key for meta_query then you need to push to that array

$args['meta_query'] = array('compare'=>'AND');

foreach ($stateArray as $state) {

    $single_state_array = array(
        'key'     => 'town_state',
        'value'   => $state,
        'compare' => '='
    );

    array_push($args['meta_query'], $single_state_array);
    /* short hand in php 7 */
    //$args['meta_query'][] = $single_state_array;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Perfect, worked like a charm! And thanks for the note on the php 7 shorthand!
That also works in php, hoping your using php 7, php 5 requires arrays to be created as "array()", php 7 you can use "$arr = [];" Good luck
0

This appears straightforward. Please tell me if I am misreading it...

$args['meta_query'] = array('compare'=>'AND');
foreach($stateArray as $val)
  $args['meta_query'][] = array('key'=>'town_state', 'value'=>$val, 'compare'=>'=');

That's all.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.