使用 category__and 和 tax_query 的 Wordpress 查询

Wordpress query using category__and with tax_query

我一直在寻找一种方法,可以在对这些类别使用​​自定义分类法的同时完成 category__and 的使用。

我有一个自定义 post 类型的 "project",在该项目中我使用了自定义分类法 "portfolio" 来对项目进行分类。

如果我使用这样的标准查询(没有结果):

                $args = array(
                    'post_type' => 'project',
                    'posts_per_page' => 12,
                    'orderby'=> 'title',
                    'order' => 'ASC',
                    'category' => $vcategory
            );  
            $loop = new WP_Query( $args );

我也试过用这样的东西:

                    $args = array(
                    'post_type' => 'project',
                    'posts_per_page' => 12,
                    'orderby'=> 'title',
                    'order' => 'ASC',
                    'tax_query' => array(
                        'taxonomy' => 'tagportfolio'
                    ),
                    'category__and' => array( $vcategory )
            );  
            $loop = new WP_Query( $args );

我有一个应用程序,允许人们使用三个下拉框按类别对 post 进行排序。盒子 1 盒子 2 盒子 3

如果从 box1 中进行选择,页面上的 post 将更新为该类别。

如果从 box2 中进行选择,页面将更新为 posts,其中包含来自 box1 && box2 的类别。

如果从 box3 中进行选择后,页面将更新为包含来自 box1 && box2 && box3 的类别的 posts。

这就是我希望使用 category__and 的原因。我知道我可以使用 terms => (the terms) 查询分类类别,尽管这不能满足我的需要,因为它会显示来自 box1 的 posts 不包含来自 box2 的类别。

我正在寻找遵循 && 逻辑而不是 || 的情况。

谢谢。

类别是分类法,但它们会得到特殊对待,因为它们是内置的。 category__and 属性 仅适用于类别分类法。

对于自定义分类法,您可以使用 tax_query 查询在该分类法中具有多个术语的帖子:

'tax_query' => array(
    array(
        'taxonomy'  => 'tagportfolio',
        'field'     => 'term_id', //change to name or slug if necessary
        'terms'     => $vcategory,
        'operator'  => 'AND'
    )
),

上述税务查询应完成与 category__and 自定义分类相同的事情。