Ok, the problem came from the fact I was looping on categories to get the matching post. What I had to do was to get the desired categories and use it in my $args
+ some adjustments.
<?php
// Get the parent cat thanks to the page's cat slug
$parent_cat = get_category_by_slug( $cat[1] );
$cats_args = array(
'type' => 'post',
'child_of' => $parent_cat->term_id
);
// child cats
$categories = get_categories( $cats_args );
// loop on the child cats to get the sub cats object
if ($categories) {
// empty array to store cats
$cats = array();
// Get the cat slugs and store it in an array
foreach ($categories as $key => $value)
$cats[] = $value->slug;
?>
<!-- THE TITLE -->
<div class="text-seperator">
<h2>Sector's programs</h2>
</div>
<ul class="sc_accordion">
<?php
$args = array(
'post_type' => 'editorial-posts',
'tax_query' => array(
'relation' => 'AND',
array(
'taxonomy' => 'category',
'field' => 'slug',
'terms' => $cats,
'include_children' => false
),
array(
'taxonomy' => 'post_tag',
'field' => 'slug',
'terms' => array( 'short-description' )
)
)
);
$program_desc_short = new WP_Query( $args );
?>
<?php if ($program_desc_short->have_posts()): while ($program_desc_short->have_posts()) : $program_desc_short->the_post() ?>
<?php
$terms = get_the_terms( get_the_ID(), 'category' );
$attached_category = array();
foreach ($terms as $term)
// in order to only get the concerned category, we check that the term is matching the cats array
if (in_array($term->slug, $cats))
foreach ($term as $key => $value)
$attached_category[$key] = $value;
?>
<li><a class="sc_accordion-btn" href="#"><?php echo $attached_category['name'] ?></a>
<div class="sc_accordion-content">
<?php the_content(); ?>
<?php edit_post_link(__('Edit This'),'<p class="edit-link">' ,'</p>'); ?>
<a href="<?php echo '/sectors/'.$attached_category['slug'] ?>" class="btn small-btn right">Read more</a>
</div>
<?php
endwhile;
endif;
wp_reset_query();
?>
</ul>
<?php
}
?>