Filter for each loop when WP_Query has no posts to show

Adding a if (has_posts() ) solved the issue which I forget to add. See the updated code in the question.