1

I have built a multidimensional array for usage in a custom Wordpress query. Although it is Wordpress I feel that this is more a basic PHP question.

The array that I have now outputs everything properly, accept for the index value. I need it to be a string instead of an integer.

Here is what I need my ouput to be

Array (
    [post_type] => property
    [posts_per_page] => -1
    [tax_query] => Array (
        [0] => Array (
            [taxonomy] => state
            [field] => slug
            [terms] => illinois
        )
        [1] => Array (
            [taxonomy] => illinois_county
            [field] => slug
            [terms] => fulton
        )
    )
)

And here is what is actually being output

Array (
    [post_type] => property
    [posts_per_page] => -1
    [0] => Array (
        [0] => Array (
            [taxonomy] => state
            [field] => slug
            [terms] => illinois
        )
        [1] => Array (
            [taxonomy] => illinois_county
            [field] => slug
            [terms] => fulton
        )
    )
)

The only difference being the key of the second array which is 0 in mine and needs to be tax_query.

To get this Array I am declaring $tax_query_array = array(); and then dropping my child arrays as needed depending on what variables are present in the url, such as $tax_query_array[] = $state_array; and $tax_query_array[] = $county_array;. Then finally calling $tax_query_array where i need the final multidimensional array output.

Only thing stopping me is the initial [0] which needs to instead be [tax_query].

Here is the full code:

$tax_query_array = array();

$tax_query_array['tax_query'][] = $state_array;
$tax_query_array['tax_query'][] = $county_array;
$tax_query_array['tax_query'][] = $price_range_array;

$taxonomy_args = array(
    'post_type' => 'property',
    'posts_per_page' => -1,
    $tax_query_array
}

Output from changing $tax_query_array[] = $county_array; to $tax_query_array['tax_query'][] = $county_array; via MikeBrant:

Array (
    [post_type] => property
    [posts_per_page] => -1
    [0] => Array (
        [tax_query] => Array (
            [0] => Array (
                [taxonomy] => state
                [field] => slug
                [terms] => illinois
            )
            [1] => Array (
                [taxonomy] => illinois_county
                [field] => slug
                [terms] => fulton
            )
        )
    )
)
Aj Troxell
  • 265
  • 1
  • 4
  • 16

1 Answers1

1

Try this code:

$taxonomy_args = array(
    'post_type' => 'property',
    'posts_per_page' => -1,
    'tax_query'=> array($state_array, $county_array, $price_range_array)
}
Ragnar
  • 4,393
  • 1
  • 27
  • 40
  • I think you need `$tax_query_array['tax_query'][] = $country_array;` – Mike Brant Dec 08 '14 at 19:52
  • But `$country_array` is already an array you don't need to create another one – Ragnar Dec 08 '14 at 19:54
  • Yes, but look at his structure. It has nested arrays inside `tax_query` element. If you take your approach, you would overwrite the entire element at that index rather than append a new entry to it. – Mike Brant Dec 08 '14 at 19:56
  • This is true. The answer just rewrites the index to the last created array. The @MikeBrant answer almost gets you there in terms of the `tax_query` key being added, but it just nests the arrays one more level deep versus renaming the first key from `0` to `tax_query`. – Aj Troxell Dec 08 '14 at 19:58
  • @MikeBrant added your output to the question – Aj Troxell Dec 08 '14 at 20:00
  • Ahh I see. I thought `$tax_query_array` was the full array that you are outputting. This was not clear from your question. I am not sure what that array name is (code examples would have helped here). What that full array is you just need to do `$full_array['tax_query'][] = $country_array`;`$full_array['tax_query'][] = $state_array;` and similar. – Mike Brant Dec 08 '14 at 20:04
  • Yes, this was not clear! $full_array['tax_query'][] is the solution. – Ragnar Dec 08 '14 at 20:10
  • Yes this seems to work. Instead of inserting the array that i was building into another array that was previously output as text, I had to build the entire thing, which is ok, and more efficient. But yes, this approach worked perfectly. – Aj Troxell Dec 08 '14 at 20:29