simple tax_query intersection
-
19-04-2021 - |
Pergunta
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;
}
Then, in the template, I loop it with foreach:
@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:
Solução
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'
],
],
Licenciado em: CC-BY-SA com atribuição
Não afiliado a wordpress.stackexchange