Error when using an array for tax_query of WP_Query arguments
-
I am trying to get all posts of custom-post-type which is attached to several terms from a custom-taxonomy. The following code in the codex says that you can use the new tax_query method. I am finding that although the code is executing and pulling results, the code is throwing exceptions for the following reason.
When the following code is executed why is strpos throwing an exception at line 1725 in the file wp-includes\query.php. The codex says that you can use an array, but the execution at this point is trying to process my array as a string.
if (is_array($countries)) { foreach ($countries as $country) { $term = get_term_by('name', htmlspecialchars($country), 'country'); $countries_array[] = $term->slug; } } if (is_array($regions)) { foreach ($regions as $region) { $term = get_term_by('name', htmlspecialchars($region), 'region'); $regions_array[] = $term->slug; } } $args = array( 'post_type' => 'vacancy', 's' => $keywords, 'tax_query' => array( 'relation' => 'OR', array( 'taxonomy' => 'country', 'field' => 'slug', 'terms' => $countries_array, ), array( 'taxonomy' => 'region', 'field' => 'slug', 'terms' => $regions_array, ) ) ); $query = new WP_Query($args);
Although if you use an array declaration direct like this no errors are thrown, am I missing something??
$args = array( 'post_type' => 'vacancy', 's' => $keywords, 'tax_query' => array( 'relation' => 'OR', array( 'taxonomy' => 'country', 'field' => 'slug', 'terms' => array('australia','united-kingdom'), ), array( 'taxonomy' => 'region', 'field' => 'slug', 'terms' => array('london','victoria'), ) ) );
I need to create the arrays dynamically though. This fix helps in wp-includes\query.php
Line: 1718
if (!is_array($term)) { if (strpos($term, '+') !== false) { $terms = preg_split('/[+]+/', $term); foreach ($terms as $term) { $tax_query[] = array_merge($tax_query_defaults, array( 'terms' => array($term) )); } } else { $tax_query[] = array_merge($tax_query_defaults, array( 'terms' => preg_split('/[,]+/', $term) )); } }
But this is not a valid solution for me as this is modifying core files.
Hope someone can see the issue here I would appreciate it.
- The topic ‘Error when using an array for tax_query of WP_Query arguments’ is closed to new replies.