Module Development

Find Data with EntityQuery for Drupal 8, 9, 10, and 11

Often when building a site in Drupal you'll find yourself wanting to display a list of nodes, or find entities created by a particular author, or locate some content based on a particular set of criteria. Rather than querying the database directly, Drupal provides a helper class, EntityQuery, to make things a bit easier. The EntityQuery class will probably look very familiar to you if you're familiar with the Database API, or the EntityFieldQuery class in Drupal 7.

In this tutorial we'll:

  • Go through several examples of using EntityQuery to find subsets of content.
  • Demonstrate how to iterate over the results of an EntityQuery query.

By the end of this tutorial, you should understand how to use entity queries to create custom sets of data from entities.