I have a custom taxonomy how-to-guide-type
and I have some terms setup in this taxonomy.
I want to display ONLY the posts assigned to each taxonomy term.
I have the following but I can only seem to display posts for the first taxonomy term not for each different one:
$returnable = [];
$custom_terms = get_terms('how-to-guide-type');
$query = new WP_Query([
'post_type' => 'cpt_how_to_guides',
'orderby' => 'date',
'order' => 'ASC',
'tax_query' => [
[
'taxonomy' => 'how-to-guide-type',
'field' => 'slug',
'terms' => $custom_terms[0]->slug,
],
],
]);
if ($query->have_posts()) {
$counter = 1;
while ($query->have_posts()) {
$query->the_post();
$returnable[] = [
'date' => get_the_date('d M Y'),
'title' => get_the_title(),
'link' => get_the_permalink(),
'content' => get_post_content(),
'count' => $counter
];
$counter++;
}
}
wp_reset_query();
return $returnable;
I have tried to for each through the terms too but this displays all posts irrespective of which term they are assigned to. I want the query to be able to dynamically figure this out without having to add the slugs for the terms in the terms =>
parameter.