How to get all nodes of a given type in Drupal?

| | 1 min read

In Drupal we can use  Drupal::entityQuery() & Node::loadMultiple() to load all nodes of given type

$nids = \Drupal::entityQuery('node')->condition('type','my_custom_type')->execute();
$nodes =  \Drupal\node\Entity\Node::loadMultiple($nids);

The below code will help you to get only published node:

$nids = \Drupal::entityQuery('node')
  ->condition('status', 1)
  ->condition('type', 'YOUR-NODE-TYPE')
  ->execute();
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);

These are the methods to get all nodes and published nodes for a type which are mainly used in drupal.

Reference

https://drupal.stackexchange.com/questions/213689/get-all-nodes-of-given-type