I have figured out. I was missing $query1->the_post();
in the while loop and wp_reset_postdata();
after the loop ends.
Final working code:
<ul class="pie-chart__legend" id="donut1">
<?php
$wcatTerms1 = get_terms('incident_type', array('hide_empty' => 0, 'parent' =>0));
foreach($wcatTerms1 as $wcatTerm1)
{
$args1 = array(
'posts_per_page' => -1,
'post_type' => 'incident',
'post_author' => $current_user->ID,
'post_status' => 'publish',
'tax_query' => array(
array(
'taxonomy' => 'incident_type',
'field' => 'term_id',
'terms' => $wcatTerm1,
'include_children' => false
)
));
$count1 = 0;
$query1 = new WP_Query($args1);
while ( $query1->have_posts() )
{
$query1->the_post();
$count1++;
}
?>
<li><em><?php echo $wcatTerm1->name; ?></em><span><?php echo $count1; ?></span></li>
<?php
wp_reset_postdata();
}
?>
</ul>