With the get_term_children()
function there is nothing to pass which will hide empty terms. You could do that in your foreach as each term has a property which holds how many posts it is assigned:
foreach( $style_categories as $style ) {
$child = get_term_by( 'id', $style, 'product_cat' );
// Skip empty terms
if( $child->count <= 0 ) {
continue;
}
}
The way it stands now you’re creating multiple queries – a better solution would be to use get_terms()
instead.
get_terms( [ 'taxonomy' => 'product_cat', 'child_of' => 29] );
get_terms
will hide the terms that have no post, you can change that with the hide_empty
argument.