WordPress的tax_query“和"操作员无法按预期运行 [英] Wordpress tax_query "and" operator not functioning as expected
问题描述
我有一个图片的自定义帖子类型,其自定义分类名为 image_tag (类似于类别,是分层的).以下是一些可能使用的标签示例:
I have a custom post type of image with a custom taxonomy called image_tag (it's hierarchical like categories). Here are some examples of the tags that might be used:
Structure (id: 25)
- House (id: 56)
- Skyscraper
Nature
- Animal
- Plant (id: 41)
因此,我想通过与"and"运算符一起选择多个标签来对图像进行细化.例如,查找带有植物和房屋的所有照片.
So, I want to drill down through the images by selecting multiple tags in conjunction with the "and" operator. For example, finding all photos with plants and houses.
$query_args = array(
'post_type' => 'image',
'tax_query' => array(
array(
'taxonomy' => 'image_tag',
'terms' => array(41, 56), // IDs of "plant" and "house"
'operator' => 'and',
),
),
);
那很好,当我尝试添加父项时,问题就开始了,例如:
That works fine, the problem begins when I try to include the parent terms, for example:
$query_args = array(
'post_type' => 'image',
'tax_query' => array(
array(
'taxonomy' => 'image_tag',
'terms' => array(25, 41), // IDs of "structure" and "plant"
'operator' => 'and',
),
),
);
然后我没有结果.我猜测是因为我使用的是和"运算符,所以Wordpress不包括结构"一词的子代.有谁知道我如何使它工作?
Then I get no results. I'm guessing that because I'm using the "and" operator, Wordpress doesn't include the children of the "Structure" term. Does anyone have an idea how I can get this to work?
推荐答案
所以它应该看起来像这样:
So it should look like this:
'relation' => 'AND',
array(
'taxonomy' => 'image_tag',
'field' => 'term_id',
'terms' => array( 25 ),
),
array(
'taxonomy' => 'image_tag',
'field' => 'term_id',
'terms' => array( 41 ),
),
),
这篇关于WordPress的tax_query“和"操作员无法按预期运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!