2016-08-23 73 views
1

我有一个自定义帖子类型图片与自定义分类称为image_tag(它是像分类像分类)。这里有可能被使用的标记的一些例子:Wordpress tax_query“and”operator not as functional expected

Structure (id: 25) 
- House (id: 56) 
- Skyscraper 
Nature 
- Animal 
- Plant (id: 41) 

所以,我想通过与“和”运营商一起选择多个标签通过图像向下钻取。例如,查找所有照片工厂 s和house s。

$query_args = array(
    'post_type' => 'image', 
    'tax_query' => array(
    array(
     'taxonomy' => 'image_tag', 
     'terms' => array(25, 41), // IDs of "structure" and "plant" 
     'operator' => 'and', 
    ), 
), 
); 

然后我没有得到任何结果:

$query_args = array(
    'post_type' => 'image', 
    'tax_query' => array(
    array(
     'taxonomy' => 'image_tag', 
     'terms' => array(41, 56), // IDs of "plant" and "house" 
     'operator' => 'and', 
    ), 
), 
); 

这工作正常,当我尝试包括父词条,例如问题开始。我猜是因为我使用“和”运算符,Wordpress不包括“结构”术语的子代。有没有人有一个想法,我怎么能得到这个工作?

+0

两个简单的问题: 1)只是为了确定。你能100%确认是否有应该退回的帖子? 2)你是否明确设置'include_children'为'true'?我相信那里至少有一个需要你这样做的错误(尽管它通常是默认的)。 – mrwweb

+0

好的:1)是的,有一些帖子标有这两个词(或其中的孩子),2)是的,我也尝试过'include_children',可悲的是,似乎没有任何影响。 – dkeeling

+0

很高兴知道。我的下一个猜测是@ hubert-popp的答案如下。我会尝试。 – mrwweb

回答

1

因此,它应该是这样的:

'relation' => 'AND', 
    array(
     'taxonomy' => 'image_tag', 
      'field' => 'term_id', 
      'terms' => array(25), 
     ), 
     array(
     'taxonomy' => 'image_tag', 
      'field' => 'term_id', 
      'terms' => array(41), 
     ), 
     ), 
0

更新:你忘了按“关闭“项”和“运营商”这样的

$query_args = array(
    'post_type' => 'image', 
    'tax_query' => array(
    array(
     'taxonomy' => 'image_tag', 
     'field' => 'term_id', 
     'terms' => array(25, 41), // IDs of "structure" and "plant" 
     'operator' => 'in' 
    ), 
), 
); 
+0

感谢您的回复!不幸的是,这对我来说似乎没有什么不同。 (我认为'term_id'是该'field'设置的默认值。) – dkeeling

+0

但使用我的代码cuz你的代码中有一些错误,比如'条款你必须像这样关闭它''也'运营商' –

+0

谢谢,我修复了这些错别字。这只是我使用的代码的一个示例,而不是完整的示例,所以我仍然得到相同的结果。 – dkeeling

0
$query_args = array(
'post_type' => 'image', 
'tax_query' => array(
    'relation' => 'AND', 
     array(
     'taxonomy' => 'image_tag', 
     'field' => 'term_id', 
     'terms' => array(25, 41), // IDs of "structure" and "plant" 
     'operator' => 'in' 
    ), 
    ), 
); 

我有一个类似的问题,试试这个!

+0

感谢您的回答!这是有效的,但不幸的是,我不在寻找的方式。此解决方案返回**结构**中的图像和**植物**中的图像(这扩大了搜索范围)。我需要的是通过返回具有**结构**和**植物**标签(或这些标签的子项)的图像来缩小搜索范围。 ......我不知道我描述它的方式是否合理,哈哈 – dkeeling

相关问题