How to get nodeid in Drupal for the current page without using the information from the page url? I.e.:

if(arg(0)=='node' && is_numeric(arg(1))){
  $nid = arg(1);

I do not want to use the above approach.


Still comes through the URL indirectly, but this is the easiest way to get it: menu_get_object.

if ($node = menu_get_object()) {
 $nid = $node->nid;

Note that if there is a node, it was already loaded by the menu system anyway and this is loaded from the static cache.

  • 2
    Note this approach makes sure the node ID is valid and the user has access to it. – user49 Jan 31 '11 at 19:29
  • i tried menu_get_object, however , it does not get any of the node attributes – user550265 Jan 31 '11 at 20:07
  • 2
    define "node attributes". menu_get_object() returns the full $node object if you are viewing a node. If you trying to get the $node elsewhere then specify that ;) – Berdir Jan 31 '11 at 20:33
  • 2
    This approach makes sure the id is valid and accessible, as @chx points out. But can also be very heavy: it often results in many DB-calls. Just last week I replaced one $node = menu_get_object() with something similar to code original question, saving 500+ database requests per page. Use with care! – berkes Jan 31 '11 at 21:12
  • 1
    berkes, menu_get_object only triggers one node_load which is static cached and on a node page it should already be there. Also, 500+ db request for a single node load? I find this hard to believe... there's something wrong. – user49 Feb 6 '11 at 5:48

Unless there is some technical reason why the arg() method is unavailable (or you NEED to check access as in other posts), the arg() method is the simplest and quickest.

If you then need additional properties you can run a node_load() to get the ID. Assuming you are trying to get the current node, this has pretty much no performance hit.


You can get the node's ID by calling $node->nid. Indeed, getting the ID from the URL is not the best solution because you may some day switch to more SEO-friendly URLs.

From this documentation, it looks like $node is defined in page.tpl.php, so it should be available to you.

  // In a template file...

  if ($node) {
    echo $node->nid;
  else {
    // you are viewing taxonomy, view, etc
  • I need to access it outside of the template file. – user550265 Jan 31 '11 at 20:24
  • Where are you trying to access it from, a module? Under what context are we talking about the "current page"? – zourtney Jan 31 '11 at 20:29
  • It should be noted that if the 'internal' url is node/12 and you use path to create a clean url the original code still works this is because the drupal node/12 path is still the url as far as the system is concerned. – mirzu Feb 1 '11 at 18:42

Easiest way to do this in Drupal 8:

$path_args = explode('/', current_path());
print $path_args[1];

Another pretty straight forward and reliable approach in D7 would be

if (isset($node->nid) && count($node->nid) > 0){
  $nodeid = $node->nid;

Zourtney's suggestion causes an undefined index error in my case.


Yes, that confused me for ages, but using the arg array should work in almost all circumstances, and Drupal takes care of the conversion from the actual url created by path or pathauto.

Your Answer

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