【问题标题】:Wordpress tax_query "and" operator not functioning as expectedWordpress tax_query“和”运算符未按预期运行
【发布时间】:2016-12-30 19:27:36
【问题描述】:

我有一个自定义的 image 帖子类型,其中包含一个名为 image_tag 的自定义分类(它类似于类别的层次结构)。以下是一些可能使用的标签示例:

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

所以,我想通过选择多个标签以及“and”运算符来深入了解图像。例如,查找所有带有植物s和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',
    ),
  ),
);

这很好,当我尝试包含父术语时问题就开始了,例如:

$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 不包括“结构”术语的子项。有谁知道我怎样才能让它工作?

【问题讨论】:

  • 两个小问题:1) 确定。你能 100% 确认有帖子应该退回吗? 2)您是否尝试过将include_children 明确设置为true?我相信至少曾经有一个错误需要您这样做(尽管它通常是默认设置)。
  • Ok: 1) 是的,有些帖子同时标记了这两个术语(或它们的子项),2) 是的,我也尝试过 include_children,遗憾的是似乎没有任何效果。
  • 很高兴知道。我的下一个猜测是下面@hubert-popp 的回答。我会试试的。
  • 是的,@hubert-popp 的解决方案确实返回了正确的结果。从同一个分类法中有多个查询很奇怪,但如果它有效,它就有效。 :) 感谢您的宝贵时间!

标签: php wordpress filtering custom-taxonomy


【解决方案1】:

更新:你忘记关闭 'terms' 和 'operator' by ' 像这样

$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'
    ),
  ),
);

【讨论】:

  • 感谢您的回复!不幸的是,这对我来说似乎没有什么不同。 (我认为term_idfield 设置的默认值。)
  • 但是使用我的代码,因为你的代码中有一些错误,比如“条款你必须像这样关闭它”“条款”和“运营商”
  • 谢谢,我修正了这些错别字。这只是我使用的代码的一个示例,而不是完整的示例,所以我仍然得到相同的结果。
【解决方案2】:
$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'
      ),
    ),
  );

我遇到了类似的问题,试试这个!

【讨论】:

  • 感谢您的回答!这是有效的,但不幸的是不是我正在寻找的方式。此解决方案返回 structure 中的图像和 plant 中的图像(扩大了搜索范围)。我需要的是通过返回同时具有 structureplant 标签(或这些标签的子标签)的图像来缩小搜索范围。 ...我不知道我描述的方式是否有意义,哈哈
【解决方案3】:

所以它应该是这样的:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-05-15
    • 2017-11-25
    • 1970-01-01
    • 2015-05-11
    • 2019-06-20
    • 2021-04-13
    • 1970-01-01
    相关资源
    最近更新 更多