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
    May 13 '12 at 16:18
  • my url is projects/{username}/{node title}, that is why I use arg(2)
    – helxsz
    May 13 '12 at 16:25
  • in drupal 7 you can't use node_load() to get node by it's title, just by nid May 13 '12 at 16:33
  • Are your titles unique?
    – Artur
    May 13 '12 at 17:20
  • 2
    Possible duplicate of How to load a node by a field value?
    – kenorb
    Aug 2 '18 at 16:24
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
    Jun 27 '11 at 18:40
  • 4
    Drupal is not a fan of CamelCase.
    – Sivaji
    Jun 28 '11 at 4:09
  • 1
    This example also just plain won't work in D7. node_load(). Have edited question tag to reflect this.
    – Chapabu
    May 28 '12 at 15:33
  • 3
    D7 version (albeit using the deprecated $conditions parameter): $nodes = node_load_multiple(array(), array('title' => 'node title'));
    – Clive
    May 28 '12 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
    Apr 23 '14 at 6:37
  • 1
    This is so long in order to load a single node;/
    – kenorb
    Jul 29 '15 at 16:14
13

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
    Apr 12 '17 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.
    – apaderno
    Dec 12 '20 at 14:16
  • @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.
    – apaderno
    Dec 12 '20 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();
0
8

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.

5

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.