How to list the latest 11-20 nodes in Drupal
I found an interesting question regarding Drupal at Codenone. One would like to list the latest 11-20 nodes in a block. He tried to use Views module with no luck. I think so. Listing the latest nodes in a block using Views is very easy but I don't see any clue to get the next 10 nodes. Anyway, it is possible to write a snippet code to fulfill this requirement.
For the first version, I use a simple query to list published nodes ordered by created
and specify a range using db_query_range()
.
$result = db_query_range(db_rewrite_sql("SELECT n.nid, n.title, n.created FROM {node} n WHERE n.status = 1 ORDER BY n.created DESC"), 10, 10);
if (db_num_rows($result)) {
echo node_title_list($result);
}
?>
However, it is not good enough yet. He would like to filter only specified taxonomy. For example, he would like to list the latest 11-20 nodes in term 22.
$str_tids = "22";
$order = "n.created DESC";
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid IN ('. $str_tids .') AND n.status = 1';
$result = db_query_range(db_rewrite_sql($sql .' ORDER BY '.$order), 10, 10);
if (db_num_rows($result)) {
echo node_title_list($result);
}
?>
So I just added INNER JOIN {term_node}
to obtain tn.tid
.
- sugree's blog
- 2140 reads
Recent comments
2 years 11 weeks ago
2 years 15 weeks ago
2 years 16 weeks ago
2 years 16 weeks ago
2 years 17 weeks ago
2 years 19 weeks ago
2 years 19 weeks ago
2 years 19 weeks ago
2 years 19 weeks ago
2 years 20 weeks ago