0

What I'm trying to achieve:

I have a taxonomy 'module_data' which is hierarchical. What I want is to use a tax query to get all posts that match a slug in an array of slugs for terms AND also match a second slug in a different array for the same taxonomy

Here's what I tried:

        $taxQuery = 
                    array (
                        'relation' => 'AND',
                        array(
                            'taxonomy' => 'regulation_country',
                            'field' => 'slug',
                            'terms' => $regulation_country,
                        ),  
                        array(
                            'taxonomy' => 'module_data',
                            'field' => 'slug',
                            'terms' => $module_type,
                        ),
                        array(
                            'taxonomy' => 'module_data',
                            'field' => 'slug',
                            'terms' => $test_type,
                        )
);

Where $module_type and $test_type are both arrays of slugs in the module_data category. This returns nothing.

Here's what works:

                $taxQuery = 
                            array (
                                'relation' => 'AND',
                                array(
                                    'taxonomy' => 'regulation_country',
                                    'field' => 'slug',
                                    'terms' => $regulation_country,
                                ),  
                                array(
                                    'taxonomy' => 'module_data',
                                    'field' => 'slug',
                                    'terms' => $module_type,
                                )
);

(where $module_type can be swapped out for $test_type and it returns either the module data matches or test_type matches respectively) but obviously this will only give me the results unconstrained by whether they match the slugs in the not included array.

Am I able to achieve what I want? to get all the regulations that match any combination of slugs from both arrays. (So where a regulation matches at least one from module_data and at least one from test_type)?

For posterity here's where the tax query gets used in get_posts:

    $resultCountries = get_posts(
        array(
            'posts_per_page' => -1,
            'post_type' => 'regulation',
            'tax_query' => $taxQuery,
            'orderby'     => 'post_title',
            'order'   => 'ASC',
        )
    );

to be clear: the result i'm looking for is posts will be returned when it can have any of the terms in the two lists, but it must have at least 1 in both

4
  • So you want all posts that have an A B or C, and and X Y or Z? E.g. A + Y or C + Z? And these aren't mutually exclusive? So A/B/X and XYZ/C count too? But not A/B or X/Y? Or are you looking for any post that has A/B/C/X/Y/Z ? Examples of what are and aren't valid would be useful – Tom J Nowell Aug 13 '20 at 10:21
  • @TomJNowell "So you want all posts that have an A B or C, and and X Y or Z? E.g. A + Y or C + Z" Precisely say Post 'testpost' is in the taxonomy 'categories' with term (slug) 'category-a' AND has the term (slug) in the same taxonomy 'category-b' then it would be returned if module_type included category-a and test_type include category-b and vice versa but NOT if module_type and test_type had a and b respectively but the post wasn't associated with a or b. – seanyt123 Aug 13 '20 at 10:29
  • So it can have any of the terms in the two lists, but it must have at least 1 in both – Tom J Nowell Aug 13 '20 at 10:33
  • @TomJNowell Exactly – seanyt123 Aug 13 '20 at 10:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.