How to include multiple content types in an entityQuery in Drupal 10

When programming in Drupal, it is often necessary to query for content of different types using the Entity Query API. In Drupal 10, the Entity Query API has been improved to make it easier to include multiple content types in a single query.

To include multiple content types in an entity query in Drupal 10, you can use the IN operator. The IN operator allows you to specify an array of content types to include in the query.

Here is an example of how to use the IN operator to include both "session" and "moment" content types in an entity query:

$query = \Drupal::entityQuery('node')
  ->accessCheck(TRUE)
  ->condition('status', 1)
  ->condition('type', ['session', 'moment'], 'IN')
  ->sort('field_session_hours', 'ASC')
  ->execute();

In this example, we are querying for nodes of type "session" and "moment". We use the condition() method to specify the content types we want to include, and we pass an array of content type strings to the type condition. By using the IN operator, we can include multiple content types in a single entity query.

 

Saved you some valuable time?

Buy me a drink 🍺 to keep me motivated to create free content like this!