• 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.

Viewing 2 replies - 1 through 2 (of 2 total)
  • I wonder if you need to declare the empty arrays?

    $countries_array = array();
    $regions_array = array();
    if (is_array($countries)) {
    // rest of your code
    Thread Starter syntaxart

    (@syntaxart)

    The solution above does not work as the array is already being declared as empty.

    I will give a little more information to this problem as well as it still is affecting me.

    I have a list of checkboxes which when check and submitted will created a url of [http://testing.dev/posts/?country[]=australia&country[]=new-zealand] when the form posts back the form checkboxes will stay checked due to the url. I need to keep this behavior. The loop will correctly retrieve the results but the php error reporting reports an error on line 1758 in wp-includes/query.php as of WordPress version 3.8. Warning: strpos() expects parameter 1 to be string, array given in C:\wamp\vhosts\testing.dev\wp-includes\query.php on line 1758.

    When I use this link [http://testing.dev/vacancy/?country=australia,new-zealand] as described on http://thereforei.am/2011/10/28/advanced-taxonomy-queries-with-pretty-urls/.

    How would you submit and build this url structure using a list of checkboxes on a form without javascript. I dont think this is possible has anyone had this issue? Or is anyone submitting multiple terms for a taxonomy and how are they doing it?

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Error when using an array for tax_query of WP_Query arguments’ is closed to new replies.