最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

tax query - simple tax_query intersection

programmeradmin1浏览0评论

I'm trying to do tax_query for posts that have a term1 in taxonomy1 AND a term2 in taxonomy2

if I do:

'tax_query' => [
  'relation' => 'AND',
  [
    'taxonomy'         => 'taxonomy1',
    'terms'            => 'term1',
  ],
  [
    'taxonomy'         => 'taxonomy2',
    'terms'            => 'term2',
  ],
],

I get all post with term1 in taxonomy1 AND all post with term2 in taxonomy2. I want the posts containing both terms: term1 and term2.

Thank you.

[EDIT. ACTUAL CODE]

First, I recover al terms of "link-category" and pass it to the template:

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

.php#L19-L26

Then, in the template, I loop it with foreach:

.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

This code collects a CPT called "link" to show it in the footer of this page: /

This is the look of the footer:

I'm trying to do tax_query for posts that have a term1 in taxonomy1 AND a term2 in taxonomy2

if I do:

'tax_query' => [
  'relation' => 'AND',
  [
    'taxonomy'         => 'taxonomy1',
    'terms'            => 'term1',
  ],
  [
    'taxonomy'         => 'taxonomy2',
    'terms'            => 'term2',
  ],
],

I get all post with term1 in taxonomy1 AND all post with term2 in taxonomy2. I want the posts containing both terms: term1 and term2.

Thank you.

[EDIT. ACTUAL CODE]

First, I recover al terms of "link-category" and pass it to the template:

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

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

Then, in the template, I loop it with foreach:

https://github/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

This code collects a CPT called "link" to show it in the footer of this page: https://stage.superbiajuridico.es/

This is the look of the footer:

Share Improve this question edited Mar 1, 2020 at 19:33 aitor asked Mar 1, 2020 at 12:50 aitoraitor 7252 gold badges8 silver badges23 bronze badges 5
  • 1 That should be the result of your example code. What's the actual code that's not giving you the result you expect? – Jacob Peattie Commented Mar 1, 2020 at 14:44
  • 2 To second JP's comment, sample code is helpful but actual code often reveals the issue. I wonder if you are leaving out the field parameter in your tax queries as well. Field will default to term_id if not explicitly defined. – jdm2112 Commented Mar 1, 2020 at 15:12
  • Thank you for the comments! I'll go to update the question with more info. – aitor Commented Mar 1, 2020 at 19:09
  • 1 As @jdm2112 pointed, if the $link and $despacho are term slugs and not IDs, then you should add 'field' => 'slug' to your tax query clauses. But if that's not the case, then you can inspect the SQL for that query: echo $link_posts->request; - what's the output? And btw, the GitHub links in the question are pointing to 404 error.. – Sally CJ Commented Mar 2, 2020 at 5:26
  • It works! Double apologies: I didn't understand the field indication the first time. And the github repository is now open. Thank you very much for the help and attention. This is an outstanding community. – aitor Commented Mar 2, 2020 at 7:28
Add a comment  | 

1 Answer 1

Reset to default 3

As jdm2112 and SallyCJ said in the comments, the query lacks of 'field' => 'slug'. So:

'tax_query' => [
  'relation' => 'AND',
  [
    'taxonomy'         => 'link-category',
    'terms'            => $link,
    'field'            => 'slug'
  ],
  [
    'taxonomy'         => 'despacho',
    'terms'            => $despacho,
    'field'            => 'slug'
  ],
],
发布评论

评论列表(0)

  1. 暂无评论