Ive been trying to figure this out for three days now, even using solutions on this site. I still can't get this working.
I have a loop that shows posts by custom taxonomy as categories. Now the taxonomy is called "charging_categories" Thus all the posts in the type case studies are shown.
But i need to sort them as separate lists by the taxonomy term from this loop. Ive tried all manner of combinations but still can't get this.
Current result: Topic 1 Question 1 Question 2 Question 3 Topic 2 Question 1 Question 2 Question 3
The desired result: Topic 1 Question 1 Question 3 Topic 2 Question 2
My code so far:
<?php
$section = get_query_var('section');
$sectionKey = get_query_var('section_key');
$categories = get_terms('charging_categories');
?>
<div id="section_<?php echo $sectionKey; ?>" class="section">
<div class="d-md-block col-12 col-md-12 charging-station-faq">
<div id="main">
<div class="container">
<div class="col-md-10 text-center">
<h2 class="white"><?php echo $fields['ch_faq_title']; ?></h2>
<p class="text-center"><?php echo $fields['ch_faq_sub_title']; ?></p>
</div>
<?php
foreach ($categories as $i => $category) { ?>
<div class="buttons-row nav nav-tabs justify-content-center" id="myTab" role="tablist">
<a class="form_change_button nav-link" data-toggle="tab" href="#<?php echo $category->name; ?>" role="tab" aria-controls="<?php echo $category->name; ?>" aria-selected="true"><?php echo $category->name; ?></a>
</div>
<?php
$args =
array(
'post_type' => 'charging_faq',
'tax_query' => array(
'taxonomy' => 'charging_categories',
'field' => 'slug',
'terms' => $category->slug
),
);
$posts = null;
$posts = new WP_Query($args);
?>
<?php
if ($posts->have_posts()) {
while ($posts->have_posts()) : $posts->the_post();
?>
<div class="accordion" id="faq">
<div class="card" style="background-color: transparent;">
<div class="card-header" id="faqhead1">
<button href="#question-<?php echo $posts->ID;?>" class="btn btn-link" data-toggle="collapse" data-target="#question-<?php echo $posts->ID;?>" role="button" aria-expanded="true" aria-controls="question-<?php echo $posts->ID;?>"><?php the_title(); ?></button>
</div>
<div id="question-<?php echo $posts->ID;?>" class="collapse" aria-labelledby="faqhead1" data-parent="#faq">
<div class="card-body">
<?php the_content(); ?>
</div>
</div>
</div>
</div>
<?php
endwhile;
wp_reset_query();
}
?>
<?php
}
?>
</div>
</div>
</div>
</div>
I found the problem, Had to nest the "tax_query" inside another array like this:
$args =
array(
'post_type' => 'charging_faq',
'tax_query' => array(
array(
'taxonomy' => 'charging_categories',
'field' => 'slug',
'terms' => $category->name
)
),
);