首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >简单tax_query交

简单tax_query交
EN

WordPress Development用户
提问于 2020-03-01 12:50:35
回答 1查看 155关注 0票数 0

我正在尝试为那些在tax_query中有term1和在taxonomy2中有term2的帖子做taxonomy1

如果我这样做了:

代码语言:javascript
复制
'tax_query' => [
  'relation' => 'AND',
  [
    'taxonomy'         => 'taxonomy1',
    'terms'            => 'term1',
  ],
  [
    'taxonomy'         => 'taxonomy2',
    'terms'            => 'term2',
  ],
],

我所有的帖子都是用term1写的,taxonomy1是用term2写的,也是用taxonomy2写的。我想要包含两个术语的帖子: term1和term2。

谢谢。

<#>

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

代码语言:javascript
复制
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

代码语言:javascript
复制
    @if (!empty($links))
      @foreach ($links as $link)
        
        @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())
          {{ $link->name }}
          
          @while ($link_posts->have_posts()) @php $link_posts->the_post();
            $link = get_field('url') @endphp
            
               {{ $link['title'] }}
            
          @endwhile
          
        @endif
        
      @endforeach
    @endif

这段代码收集了一个名为"link“的CPT,以显示在这个页面的页脚:https://stage.superbiajuridico.es/中。

这是页脚的外观:

EN

回答 1

WordPress Development用户

回答已采纳

发布于 2020-03-02 07:30:44

正如jdm2112和SallyCJ在评论中所说的那样,查询缺少'field' => 'slug'。所以:

代码语言:javascript
复制
'tax_query' => [
  'relation' => 'AND',
  [
    'taxonomy'         => 'link-category',
    'terms'            => $link,
    'field'            => 'slug'
  ],
  [
    'taxonomy'         => 'despacho',
    'terms'            => $despacho,
    'field'            => 'slug'
  ],
],
票数 3
EN
页面原文内容由WordPress Development提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://wordpress.stackexchange.com/questions/359791

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档