31

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

How do I do that?

  • 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 – abd 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));
  }
  • 2
    @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
  • 3
    Drupal is not a fan of CamelCase. – Sivaji Jun 28 '11 at 4:09
  • 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
  • 2
    D7 version (albeit using the deprecated $conditions parameter): $nodes = node_load_multiple(array(), array('title' => 'node title')); – Clive May 28 '12 at 15:44
46

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'])));
  }
  • node_load(array('title' => 'node title', 'type' => 'page')) - this will obsolete soon, so this answer is correct. – Nikit Apr 23 '14 at 6:37
  • This is so long in order to load a single node;/ – kenorb Jul 29 '15 at 16:14
11

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

9

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();
  • you welcome :D. – abd May 13 '12 at 16:49
4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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