Community Documentation

Display List of (x) Node Titles, Sorted by Newest Content

Last updated January 8, 2007. Created by pwolanin on July 27, 2006.
Edited by WeRockYourWeb.com. Log in to edit this page.

Note from the moderator: Thank you for sharing the snippet with the Drupal community. In its current state the snippet might present security risks. See Writing secure code for a background on the most common problems.

Specifically, the snippet

  • bypasses node access restrictions; you should usually pass queries through db_rewrite_sql.

This displays a list of node titles, sorted by creation date in descending order (newest nodes are listed first). I also have a filter in there for my "301" (redirect) pages that I don't want listed. Simply modify or remove AND node.title NOT LIKE '301%' if you don't need this. You could also add a filter such as AND node.type = 'page' to only list nodes of a certain type ('page' in this example).

<?php
/**
* Displays list of node titles, sorted by content creation date (newest first)
*
* To change the length of the list, change the $list_no value
*
* This snippet was tested with Drupal 4.7, but should work with 4.5+
*
*/
$list_no =10;
$sql = "SELECT node.title, node.nid FROM node WHERE node.status = 1 AND node.title NOT LIKE '301%' ORDER BY node.changed DESC LIMIT $list_no";
$result = db_query($sql);
while (
$anode = db_fetch_object($result)) {
$links[] = l($anode->title, "node/". $anode->nid);
}
return
theme_item_list($links);
?>

Contact me with any questions.
Alex
----------
Contract Web Development

Comments

Code for Drupal 6?

This code is for Drupal 4, will it work correctly for Drupal 6?

If no, then how it should be changed for 6th version of Drupal?

• •
¤ Data Recovery | ¤ Halo 3 | ¤ Melodiya Records

will this work on d6?

will this work on d6?

About this page

Drupal version
Drupal 4.7.x

Reference

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.
nobody click here