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 Answer
Reset to default 3As 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'
],
],
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$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