34

I only know the title of a node, and I want to load the node using node_load().

How do I do that?

5
  • If its a normal Page you should get NID in arg(1) it will be node/X for all pages. and node_load() works on only NID
    – GoodSp33d
    Commented May 13, 2012 at 16:18
  • my url is projects/{username}/{node title}, that is why I use arg(2)
    – helxsz
    Commented May 13, 2012 at 16:25
  • in drupal 7 you can't use node_load() to get node by it's title, just by nid Commented May 13, 2012 at 16:33
  • Are your titles unique? Commented May 13, 2012 at 17:20
  • 2
    Possible duplicate of How to load a node by a field value?
    – kenorb
    Commented Aug 2, 2018 at 16:24

6 Answers 6

17

In Drupal 6, you can use the following code.

$node = node_load(array('title' => 'node title'));

If you know the node's type as well, then you can use the following code.

$node = node_load(array('title' => 'node title', 'type' => 'page'));

node_load() always returns a single node. That is because the function doesn't loop over the database query result resource returned from db_query().

  if ($revision) {
    array_unshift($arguments, $revision);
    $node = db_fetch_object(db_query('SELECT ' . $fields . ' FROM {node} n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN {node_revisions} r ON r.nid = n.nid AND r.vid = %d WHERE ' . $cond, $arguments));
  }
  else {
    $node = db_fetch_object(db_query('SELECT ' . $fields . ' FROM {node} n INNER JOIN {users} u ON u.uid = n.uid INNER JOIN {node_revisions} r ON r.vid = n.vid WHERE ' . $cond, $arguments));
  }
4
  • 3
    @sokratis Keep in mind that titles don't have to be unique and can be changed at any time. If you can switch to referencing by node id, that would be safer.
    – David L
    Commented Jun 27, 2011 at 18:40
  • 4
    Drupal is not a fan of CamelCase.
    – Sivaji
    Commented Jun 28, 2011 at 4:09
  • 1
    This example also just plain won't work in D7. node_load(). Have edited question tag to reflect this.
    – Chapabu
    Commented May 28, 2012 at 15:33
  • 3
    D7 version (albeit using the deprecated $conditions parameter): $nodes = node_load_multiple(array(), array('title' => 'node title'));
    – Clive
    Commented May 28, 2012 at 15:44
50

In Drupal 7, the parameters for node_load() has been changed. To get the nodes that match some conditions, you should use the EntityFieldQuery class.

$query = new EntityFieldQuery();


 $entities = $query->entityCondition('entity_type', 'node')
  ->propertyCondition('type', 'your_node_type')
  ->propertyCondition('title', 'your node title')
  ->propertyCondition('status', 1)
  ->range(0,1)
  ->execute();

  if (!empty($entities['node'])) {
    $node = node_load(array_shift(array_keys($entities['node'])));
  }
2
  • 1
    node_load(array('title' => 'node title', 'type' => 'page')) - this will obsolete soon, so this answer is correct.
    – Nikit
    Commented Apr 23, 2014 at 6:37
  • 1
    This is so long in order to load a single node;/
    – kenorb
    Commented Jul 29, 2015 at 16:14
14

Drupal 7

Here is a simpler way (as already suggested by Clive and at SO):

$nodes = node_load_multiple(NULL, array("title" => "Foo Bar"));
$node = current($nodes);

and much easier to remember than using EntityFieldQuery class.

See also: Programmatically getting nodes by title in Drupal 7 at SO

3
  • 3
    nice approach, unfortunalty intented to die, as exposed in [api.drupal.org/api/drupal/modules%21node%21node.module/function/… (function node_load_multiple) @todo Remove $conditions in Drupal 8.
    – Augusto
    Commented Apr 12, 2017 at 19:35
  • This code is not compatible with Drupal 8, where the second argument of node_load_multiple() is a Boolean value telling Drupal to reset the internal cache used for the loaded entities.
    – avpaderno
    Commented Dec 12, 2020 at 14:16
  • 1
    @augusto That isn't a reason sufficient not to use the code shown here, though. Even using the EntityFieldQuery class would require changes to port the code to Drupal 8.
    – avpaderno
    Commented Dec 12, 2020 at 14:28
11

In Drupal 7, you can use code similar to the following one.

$result = db_query("SELECT n.nid FROM {node} n WHERE n.title = :title AND n.type = :type", array(":title"=> $title, ":type"=> $type));  
$nid = $result->fetchField();
1
  • Doesn't title stored in the node_revision table?
    – user5858
    Commented Nov 12, 2022 at 20:12
11

In Drupal 8, run this:

$nodes = \Drupal::entityTypeManager()
  ->getStorage('node')
  ->loadByProperties(['title' => $title]);
foreach ( $nodes as $node ) {
  $node->doSomething(...);
}

$node will be a fully instantiated node object.

6

Also in Drupal 8

$nids = \Drupal::entityQuery('node')
->condition('title', 'YourNodeTitle')
->sort('nid', 'DESC')
->execute();

and next:

$node = \Drupal\node\Entity\Node::load(HEREYOUNODEID);

Dont forget you need

use Drupal\node\Entity\Node;

Not the answer you're looking for? Browse other questions tagged or ask your own question.