Hello,

I've been looking at examples of using node_load() and most examples in the API show loading a specific node by nid. http://api.drupal.org/api/drupal/modules--node--node.module/function/nod...

How would I go about loading the latest published node of a certain type?
I'm just trying to figure out the 'conditions' format to say.

$my_node = $node_load(array('type' => $node->type, 'date' => ??? latest / newest post ???));

Any direction / suggestions or examples appreciated.

Thank you

Comments

hilarudeens’s picture

hi,
I have just cracked the "admin/content" callback and found the solution...

<?php
   $latestnids=
          db_select('node', 'n')->extend('PagerDefault')
          ->fields('n',array('nid'))            
          ->limit(1)
          ->orderBy('n.changed','DESC')
          ->condition('n.type','your_content_type')
          ->condition('n.status',1)
          ->execute()
          ->fetchCol();
    $latestnodes=node_load($latestnids);
    print "<pre>"; print_r( $latestnodes); print "</pre>";
....
..
?>

good luck...,

internets’s picture

Thanks for the suggestion. That code appears to be for Drupal 7.

For Drupal 6 I found a way to get the info I wanted using db_query... There could be a better way but this works for me.

My goal is just to check if the current node is the most recent from node-type.tpl.php, if it is, then do something special. So I'll see if the loaded $nid->nid is the same as $node->nid and if so, show a special 'new' notice' otherwise show as a normal node.

So here is what I'm using to get the newest node for now.

$result = db_query("SELECT nid FROM {node} WHERE status = 1 AND type = '%s' ORDER BY `created` DESC", $node->type);
$nid = db_fetch_object($result);
print $nid->nid;
if ($nid->nid == $node->nid) {
	$latest_node = true;
}
else {
	$latest_node = false;
}