tags:

views:

91

answers:

2

Is there a function in the Drupal API that I can use to get nodes by author ID?

I am trying to create a block that shows the current user a list of their authored pages and I'm having a surprisingly difficult time with it.

+3  A: 

This is easily done with SQL:

global $user;
$items = array();
$result = db_query("SELECT nid, title FROM {node} WHERE uid = %d", $user->uid);
while ($row = db_fetch_object($result)) {
  $items[] = l($row->title, 'node/' . $row->nid);
}
return theme('item_list', $items, NULL, 'ul');

The above code in a custom block should do the trick. Just remember not to cache it.

googletorp
I had been looking for an API function as I am working in such a situation where I can't directly access the database this drupal installation is running on. So I couldn't see records or column names or anything. Thanks so much for such a complete answer!
jordanstephens
You can use the backup and migrate module to take a copy of the db into your local enviroment, if you need to give it a look.
googletorp
+3  A: 

You could be using the Views module for this. It generates pages, blocks, feeds, and more via a web UI that lets you construct a database query. Very slick, and heavily used on most Drupal sites.

ceejayoz
Agreed, this seems like a perfect use case for Views.
Greg

related questions