simple tax_query intersection

时间:2020-03-01 作者:aitor

我正在尝试对taxonomy1中有term1,taxonomy2中有term2的帖子进行tax\\u查询

如果我这样做:

\'tax_query\' => [
  \'relation\' => \'AND\',
  [
    \'taxonomy\'         => \'taxonomy1\',
    \'terms\'            => \'term1\',
  ],
  [
    \'taxonomy\'         => \'taxonomy2\',
    \'terms\'            => \'term2\',
  ],
],
我在taxonomy1中获得所有带有term1的帖子,在taxonomy2中获得所有带有term2的帖子。我希望帖子同时包含两个术语:term1和term2。

非常感谢。

[EDIT. ACTUAL CODE]

首先,我恢复“链接类别”的所有术语并将其传递给模板:

public function links()
{
    $terms = get_terms(array(
        \'taxonomy\' => \'link-category\',
        \'hide_empty\' =>true,
        ));
    return $terms;
}

https://github.com/aitormendez/superbiajuridico/blob/master/web/app/themes/sj/app/Controllers/App.php#L19-L26

然后,在模板中,我用foreach循环它:

https://github.com/aitormendez/superbiajuridico/blob/master/web/app/themes/sj/resources/views/partials/footer.blade.php#L14-L28

    @if (!empty($links))
      @foreach ($links as $link)
        <div class="col bloque py-3">
        @php
        $args = [
          \'post_type\'              => [\'links\'],
          \'post_status\'            => \'publish\',
          \'tax_query\'              => [
            \'relation\' => \'AND\',
            [
              \'taxonomy\'         => \'link-category\',
              \'terms\'            => $link,
            ],
            [
              \'taxonomy\'         => \'despacho\',
              \'terms\'            => $despacho,
            ],
          ],
        ];
        $link_posts = new WP_Query($args);
        @endphp
        @if ($link_posts->have_posts())
          <h3>{{ $link->name }}</h3>
          <ul>
          @while ($link_posts->have_posts()) @php $link_posts->the_post();
            $link = get_field(\'url\') @endphp
            <li>
              <a href="{{ $link[\'url\'] }}" target="{{ $link[\'target\'] }}"> {{ $link[\'title\'] }}</a>
            </li>
          @endwhile
          </ul>
        @endif
        </div>
      @endforeach
    @endif
此代码收集一个名为“link”的CPT,将其显示在此页面的页脚中:https://stage.superbiajuridico.es/

这是页脚的外观:

footer

1 个回复
最合适的回答,由SO网友:aitor 整理而成

正如jdm2112和SallyCJ在评论中所说,查询缺少\'field\' => \'slug\'. 因此:

\'tax_query\' => [
  \'relation\' => \'AND\',
  [
    \'taxonomy\'         => \'link-category\',
    \'terms\'            => $link,
    \'field\'            => \'slug\'
  ],
  [
    \'taxonomy\'         => \'despacho\',
    \'terms\'            => $despacho,
    \'field\'            => \'slug\'
  ],
],

相关推荐