使用 category__ 和 tax_query 进行 Wordpress 查询

GDT*_*GDT 4 php wordpress taxonomy

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

我有一个“项目”的自定义帖子类型,在该项目中,我使用了一个自定义分类“组合”来对项目进行分类。

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

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

我也尝试使用这样的东西:

                    $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 );
Run Code Online (Sandbox Code Playgroud)

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

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

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

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

这就是我希望使用 category__and 的原因。我知道我可以使用术语 =>(术语)查询分类类别,但这不会满足我的需求,因为它会显示来自 box1 的帖子,但不包含来自 box2 的类别。

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

谢谢你。

Mat*_*ley 5

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

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

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

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