Get top 5 recent post by taxonomy term
First of all, you say:


  I have started overwriting Twig files in Drupal 8.


I hope you mean "overriding" Twig files.

Rule #1: Don't hack core.

Secondly, as leymannx suggests in the comment above, this is better accomplished using Views rather than in a Twig template.