WordPress: get_terms() returns 0, even though there are posts in the terms in question

On a client's site where we bulk save posts via an api using wp_insert_post() and a custom import, I had discovered a bug where terms are indeed listed on the new posts, but it doesn't update the term_taxonomy count, which meant that a term that had posts was listed as having none, which could result in get_posts returning 0 objects.

One solution is this little feature for wp_update_term_count_now() which updates the term count immediately. My function looks like this:

function reset_counts() {
	$tax = 'bilmaerke';
	$terms = get_terms( [ 'taxonomy' =>$tax, 'hide_empty' => false,'fields' => 'ids' ] ) );
	foreach ( $terms as $term ) {
		wp_update_term_count_now( $terms, $tax);

The above function runs via init as a one-time call, but you could also set it to run via a wp_cron or a normal cron job, so that you continuously ensure that posts are retrieved for the correct term_count for each term.