Get node by title in Drupal 8

0
2071
Get node by title in Drupal 8
Get node by title in Drupal 8

In this post, I will show you a tip to get node by title in Drupal 8, which also works in Drupal 9.

To achieve that, see the following code snippet:

// load all node IDs based on title
$nids = \Drupal::entityQuery('node')
->condition('title', 'Node Title Here')
->sort('nid', 'DESC')
->execute();

// load a single node
$node = \Drupal\node\Entity\Node::load($nids[0]);

// load multiple nodes at a time
$nodes = \Drupal\node\Entity\Node::loadMultiple($nids);

Have fun!