How to get the path of a node from the node ID in Drupal

By David Pratt / Tags: , / 11 Comments / Published: 03-12-09

I’ve just stumbled across a very useful internal function in Drupal that I thought I’d share here.

If you have a node that you know the node ID for, but you don’t know what the full clean url is (or how to generate it), then there is a handy function you should know about called drupal_lookup_path() that will get the full clean (pathauto) path for you. For example, a typical Drupal relative path such as:

/node/9999

Is easy to generate if you know the node ID, but if you pass it through the drupal_lookup_path function like so:

drupal_lookup_path('alias',"node/".$node->nid)

Then you will get the full clean path of the node based on the rules set out in the pathauto module. For example:

/band/future-of-the-left

This is very important for SEO. If the exact same content/page can exist on two different urls on your site, then you will run into problems with duplicate content and will have to start using things like the canonical link element to fix a problem that shouldn’t have been created in the first place.

Category: Tech

Tags: ,

Posted: on December 3rd, 2009 at 1:29 pm.

Feeds: RSS 2.0

11 Responses to “How to get the path of a node from the node ID in Drupal”

Reece Marsland December 9th, 2009 at 7:49 pm

And to make doubly sure you can add the global redirect module (http://drupal.org/project/globalredirect) which will route all non aliased urls to aliased urls (if they exist!) :-)

David Pratt December 9th, 2009 at 11:46 pm

Ah sneaky. I’ll look into that.

Reece December 18th, 2009 at 1:12 am

also the l() function invokes the drupal_lookup_path function (i think) so if you know the nid and title you could do something like this:

l($node->title, ‘node/’.$node->nid)

which will actually turn it into a nice clean anchor with an aliased path. :D

Julio March 2nd, 2010 at 1:17 am

Thanks for sharing this! I was looking for it and found it very handy.

Tajinder Singh July 12th, 2010 at 11:28 am

If you are using views, then it is better to use $GLOBALS to access both values.

Otherwise $base_url came blank to me.

print $GLOBALS[‘base_url’] . $GLOBALS[‘base_path’];

You can further concatenate

drupal_lookup_path(‘alias’,”node/”.$data->nid);

to get complete path to any node.

as

print $GLOBALS[‘base_url’] . $GLOBALS[‘base_path’] . drupal_lookup_path(‘alias’,”node/”.$data->nid);

Thanks

Milan April 12th, 2011 at 4:06 pm

This is working for getting aliases in current language. But when I try to get alias of page in other language (I’m making custom language switch link) it’s not working?
Any idea why?

Mossill May 17th, 2011 at 5:08 pm

Awesome! i was trying to re-build my own url with regular expressions using custom php field in views. this saves alot of time and is efficent. thanks

Luca October 25th, 2011 at 3:26 pm

Thank you!! searching it!

Alon December 4th, 2011 at 11:12 am

Thanks ! was looking for this.

Rich March 19th, 2013 at 3:29 pm

thanks so much for this… I’d add you immediately to my Google Reader, that is if I still had a Google Reader :-(

Leave a reply