8.3.x node.module node_load($nid = NULL, $reset = FALSE)
8.0.x node.module node_load($nid = NULL, $reset = FALSE)
8.1.x node.module node_load($nid = NULL, $reset = FALSE)
8.2.x node.module node_load($nid = NULL, $reset = FALSE)
8.4.x node.module node_load($nid = NULL, $reset = FALSE)
4.6.x node.module node_load($conditions, $revision = NULL, $reset = NULL)
4.7.x node.module node_load($param = array(), $revision = NULL, $reset = NULL)
5.x node.module node_load($param = array(), $revision = NULL, $reset = NULL)
6.x node.module node_load($param = array(), $revision = NULL, $reset = NULL)
7.x node.module node_load($nid = NULL, $vid = NULL, $reset = FALSE)

Loads a node object from the database.

Parameters

$nid: The node ID.

$vid: The revision ID.

$reset: Whether to reset the node_load_multiple cache.

Return value

A fully-populated node object, or FALSE if the node is not found.

87 calls to node_load()
BookTestCase::testBookDelete in modules/book/book.test
Tests the access for deleting top-level book nodes.
book_admin_edit_submit in modules/book/book.admin.inc
Form submission handler for book_admin_edit().
book_block_view in modules/book/book.module
Implements hook_block_view().
book_export in modules/book/book.pages.inc
Menu callback; Generates representations of a book page and its children.
book_export_html in modules/book/book.pages.inc
Generates HTML for export when invoked by book_export().

... See full list

File

modules/node/node.module, line 963
The core that allows content to be submitted to the site. Modules and scripts may programmatically submit nodes using the usual form API pattern.

Code

function node_load($nid = NULL, $vid = NULL, $reset = FALSE) {
  $nids = (isset($nid) ? array($nid) : array());
  $conditions = (isset($vid) ? array('vid' => $vid) : array());
  $node = node_load_multiple($nids, $conditions, $reset);
  return $node ? reset($node) : FALSE;
}

Comments

tamerzg’s picture

node_load() with params is deprecated in D7 in favor of EntityFieldQuery().

If you had a code in D6 to load a node using an array of conditions to match against in the database query like this:

$param = array(
    'type' => 'your_node_type',
    'title' => 'your node title',
    'status' => 1,
);
$node = node_load($param);

then in D7 you ll have to use:

  $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'])));
  }
akroplas’s picture

Tamerzg, thank you for very good example. It's very helpful!
Using:

array_shift(array_keys($entities['node']))

gets me an notice: "Strict warning: Only variables should be passed by reference in ..."
I think It should be replaced with:

current(array_keys($entities['node']))

I've found a solution
here

MD3’s picture

Thanks for the awesome example! I just don't understand the advantage of all that when I can simply do:

$nid = db_select('node', 'n')->condition('n.type', 'your_node_type')->condition('n.title', 'your node title')->condition('n.status', 1)->fields('n', array('nid'))->execute()->fetchField();


if($nid !== FALSE) { $node = node_load($nid); }

Can you enlighten me on the advantages of using EntityFieldQuery() ?

frob’s picture

The primary benefit is that you're not just selecting a node from the db. You are instead selecting entities.

For more info on this checkout http://api.drupal.org/api/drupal/includes%21entity.inc/class/EntityField...

Basically this allows another module a hook point to alter the entity.

lmeurs’s picture

A huge benefit is that you cannot only query for 'property conditions' (ei. status, title), but also 'field conditions' as in field values (ie. 'field_tags') without having to define the table joins yourself.

See https://drupal.org/node/1343708 for a full explanation.

dx007’s picture

Thank you very much, Ive been looking for hours to make this for loading every node of a certain content type and pass them to entity_metadata_wrappers.

cubeer’s picture

Actually, i connect to a drupal6 's database named 'drupal6DB',

and i want to call the drupal6's node_load() to fetch the record from the drupal6DB,

So, how can i use the code ?

frob’s picture

You will not be able to do a node load from a D6 db with D7. They are completely different apis. I would suggest doing a Drupal 2 Drupal migration and bring all the D6 nodes into D7.

Tengu’s picture

If you only have an revision id you can use:

node_load(null, $revision_id);

lookatthosemoose’s picture

If the node you wish to load is the current node being loaded by the page, then you can use menu_get_object() which is much less resource intensive, mainly because it is cached.

http://api.drupal.org/api/function/menu_get_object/7

mxh’s picture

You are right, menu_get_object() should be used when you want to get the node which is loaded on the current page. But it is not less resource intensive, it's even more hungry than node_load(), see this comparison. node_load() also uses caching.

When you use node_load() on hook_node_update(), you should reset node_load_multiple cache ($reset = TRUE) to get valid node data. See this comment for more information.

mxh’s picture

Sorry, comment link from above is broken. Once again: comment

msypes’s picture

I hit upon this while refactoring; thought it would help someone out:
Using a loop to load nodes, modify some details and save copies. When a particular node was loaded a second time, it brought back zero instead of an array:

// Load a node
$node = node_load($nid);

// Establish as a new node
unset($node->nid);
$node->is_new = true;

/* do other mods.*/
// Save the copy
node_save($node);

With the above code within a loop that happened to bring up the same $nid twice, the second iteration failed, because node_load returns 0 (zero) instead of a node object.

After a lot of testing, I determined this was caused by the unsetting of $node->nid and was alleviated by reseting the "load multiple cache" thusly: $node = node_load($nid, null, true);

shabana.navas’s picture

This definitely needs to be documented more. I spent quite some time fiddling with this issue. It's amazing that putting it into a loop and unsetting the nid causes such a huge mess. Thanks for the help, really appreciate it!

TheUnseen’s picture

You're supposed to know that (since PHP5) objects are generally always references for performance reasons.