Category objects returned by API have count of posts on them in count
field. You should simply check that and skip rest of the iteration for those that have no posts.
Something like:
foreach( $categories as $category ) {
if( 0 == $category->count ) {
continue;
}
Scratch that. If I get it right this time what you really need is to check if you got any posts before you output category header. Something like (don’t use query_posts()
by the way, it’s trouble):
$stuff = new WP_Query( $args );
if ( $stuff->have_posts() ) {
?><a href="#" class="list-group-item active"><?php echo $category->name; ?></a><?php
while ( $stuff->have_posts() ) : $stuff->the_post();
// posts output
endwhile;
}