最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Can I create a tax query to return posts that match two slugs from same taxonomy

programmeradmin0浏览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

发布评论

评论列表(0)

  1. 暂无评论