Home > 6 > load recent nodes with node_load()

load recent nodes with node_load()

August 4Hits:0
Advertisement

How can I load recent nodes with node_load()?

I used the following code, but I can't edit the links because it creates a list where they are inside it. I want to load the node IDs, and then use node_load() to load each node.

$output = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.type = '%s' AND n.status = 1 ORDER BY n.created DESC"), story, 0, 10)); print $output; 

How is that possible?

Answers

Using only node_load() you cannot get a list of the most recent nodes, as node_load() returns a single node object for the node that matches some criteria; it's not a function that returns a list of nodes.

The faster, but a little inefficient, way to get a list of more recent nodes is to:

  • Get the list of node IDs associated with the most recent nodes
  • Call node_load() for each of the node IDs obtained from the first step

You could use code similar to the following one.

   $nodes = array();
   $result = db_query_range("SELECT n.nid FROM {node} n ORDER BY n.created DESC", array(), 0, $number_of_posts);

   while ($nid = db_result($result)) {
     $nodes[] = node_load($nid);
   }

After that code is executes, $nodes will contains the $number_of_poststh recent nodes.

Related Articles

Copyright (C) 2017 ceus-now.com, All Rights Reserved. webmaster#ceus-now.com 14 q. 0.552 s.