Drupal - Get all nodes of given type

You can use Drupal::entityQuery() & Node::loadMultiple() to load all the nodes of your given type:

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

Another way to do this would be using this snippet of code:

// Set properties to filter.
$values = [
  'type' => 'page',
];

// Get the nodes.
$nodes = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->loadByProperties($values);

Its actually very easy,

\Drupal::entityTypeManager()->getStorage('node')
  ->loadByProperties(['type' => 'content_type', 'status' => 1])

If you would like all nodes also unpublished just use:

\Drupal::entityTypeManager()->getStorage('node')
  ->loadByProperties(['type' => 'content_type'])

Tags:

Nodes

8