-1

trying to add the array below into an existing one with array_push:

'tax_query' => array(
        'relation' => 'and',
        array(
            'taxonomy' => 'type',
            'field' => 'slug',
            'terms' => 'national'
            )
    )

the existing array:

$args = array(
        'post_type' => 'events',       
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'events_start-date',
                'value' => date('YYYY-mm-dd'),
                'type' => 'CHAR',
                'compare' => '>='
            )
        )
);

i have tried this code with no luck:

array_push(
$args, array('tax_query'=> array('relation' => 'and', 
array('taxonomy' => 'prince-range','field' => 'slug','terms' => '99')
)));
Codex73
  • 5,690
  • 11
  • 56
  • 76

3 Answers3

1

You’re over complicating it.

$args['tax_query'] = array(
     'relation' => 'and',
          array(
               'taxonomy' => 'type',
               'field' => 'slug',
               'terms' => 'national'
         )
     );
Phillip Berger
  • 2,317
  • 1
  • 11
  • 30
  • What if you already have existing array you want to merge to the current? – zerkms May 29 '13 at 01:53
  • This will merge them. It’ll add the new data to an array key called `tax_query` automatically. – Phillip Berger May 29 '13 at 01:54
  • what if you just have `$array_to_append`? Where did you get `tax_query` string from? Why do you think it is hardcoded? – zerkms May 29 '13 at 01:55
  • That will add it to the end of the array with the next available numeric array key (keys start at 0 in PHP). – Phillip Berger May 29 '13 at 01:56
  • you didn't get my point. Read the question once again and answer: why do you think `tax_query` is hardcoded in your code? – zerkms May 29 '13 at 01:57
  • it's not, but my question is still here: what if the array to merge is dynamic. How would you know `tax_query`? – zerkms May 29 '13 at 01:58
  • 1
    Thank you Phillip, you're completely right! I was over complicating it... this works excellent. – Codex73 May 29 '13 at 01:58
  • @zerkms, depends on the situation of course. You could easily substitute another `$args[’$variable’]` if you wanted, but in this case the array to be added was named `tax_query` so I answered the question based on that. – Phillip Berger May 29 '13 at 01:59
  • `You could easily substitute another $args[’$variable’]` --- what if there are more than one items in array? :-S I agree the question is asked *terribly*, but it's too specific answer :-S – zerkms May 29 '13 at 02:00
  • Guysm thanks for your help both, i appreciate it. sorry my question wasn't clear – Codex73 May 29 '13 at 02:02
  • 1
    @zerkms: What if the sky is falling? You deal with each situation as it arises. – Phillip Berger May 29 '13 at 02:02
  • @Phillip: assuming that an array will always have some key as soon as you just need a merge is weird. – zerkms May 29 '13 at 02:04
0
$merged = array_merge($args, $array_to_merge);

As soon as you're merging 2 arrays - you need to use array_merge, not array_push

Online demo: http://ideone.com/jHQipV

zerkms
  • 249,484
  • 69
  • 436
  • 539
0
$arr = array('tax_query' => array(
        'relation' => 'and',
        array(
            'taxonomy' => 'type',
            'field' => 'slug',
            'terms' => 'national'
            )
    )
);

$args = array(
        'post_type' => 'events',       
        'meta_query' => array(
            'relation' => 'AND',
            array(
                'key' => 'events_start-date',
                'value' => date('YYYY-mm-dd'),
                'type' => 'CHAR',
                'compare' => '>='
            )
        )
);

$args[] = $arr;

var_dump on $args:

array(3) { ["post_type"]=> string(6) "events" ["meta_query"]=> array(2) { ["relation"]=> string(3) "AND" [0]=> array(4) { ["key"]=> string(17) "events_start-date" ["value"]=> string(26) "2013201320132013-0505-2929" ["type"]=> string(4) "CHAR" ["compare"]=> string(2) ">=" } } [0]=> array(1) { ["tax_query"]=> array(1) { ["relation"]=> array(3) { ["taxonomy"]=> string(4) "type" ["field"]=> string(4) "slug" ["terms"]=> string(8) "national" } } } } 
Ciaran
  • 169
  • 2
  • 8