simple tax_query intersection

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.com/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.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

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:

Topic tax-query multi-taxonomy-query Wordpress

Category Web


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'
  ],
],

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.