It seems you need to rest the post data before return the $postCount. Before return it, add the follow:
wp_reset_postdata();
—
There is a difference between wp_reset_query()
and wp_reset_postdata()
, as explained here.
wp_reset_query()
– ensure that the main query has been reset to the original main query.wp_reset_postdata()
– ensures that the global $post has been restored to the current post in the main query.