EntityQuery example in Drupal 8
9 March 2022
The excellent views module (in Drupal Core) is probably sufficient most of the time but if you'd like to create a list of content programmatically you could also use a EntityQuery.
Let's say you'd want a list of blog items ... In your YOURTHEMENAME.theme file of your template you could add the following code to load the nodes and add a view mode as well (to use a specific template, in this case a teaser).
Now each page.html.twig has a variable {{ blog }} that you can render ...