Last updated 14 March 2009. Created on 11 April 2007.
Edited by hplc, fax8. Log in to edit this page.

The code below let display on a block (or a php page) a block with the latest X (configurable in the code) nodes associated with some taxonomy terms.

Let's imagine you have taxonomy terms "fruit" and "vegetable" which have term id as 49 and 50.
Then the code to display latest 10 entries in those terms is

// comma separated lists of terms tid to display nodes
$terms = "49,50";

// the number of nodes to show
$count = 10;


$sql = "SELECT n.title, n.nid FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid in ($terms) AND n.status=1 ORDER BY n.created DESC";
$result = db_query_range(db_rewrite_sql($sql), 0, $count);
if ($result) {
  return node_title_list($result);
}
return t('No nodes available.');

Looking for support? Visit the Drupal.org forums, or join #drupal-support in IRC.

Comments

nadiana’s picture

You can avoid duplicated entries by adding "distinct"

$sql = "SELECT distinct n.title, n.nid FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid in ($terms) AND n.status=1 ORDER BY n.created DESC";

DrupalDummy’s picture

It worked in Version 6 with if (db_result($result))

Can someone please tell me how I can print teasers under each title in the above code?

Thanks.

adisetiawan’s picture

something like

if (db_result($result)) {
  while ($data = db_fetch_object($result)) {
    $node = node_load($data->nid);
    //format this result array as you want
     print_r($node);
  }
}

Adi Setiawan
--------------
http://ex3me.org

hplc’s picture

The newest node posted can't be display by this PHP block snippet, while the others are ok?

Why?

My Drupal version is 6.10

hplc’s picture

As DrupalDummy said using: if (db_result($result)) in Drupal 6.10
But it can't display all the newest node, the newest node can't display.

So I changed the line to:
if ($result)

This works!

morisy’s picture

Plugged it into a block and it worked beautifully. Thanks to all the community members who tweaked it!

I wanted to padded it with some before and after text:

blah blah blah:

  • Piece 1
  • Piece 2
  • Piece 3

Blah blah.

But no matter what I do, the text after piece 3 fails to display. Is this a drawback of using PHP snippets? Is there something in this particular snippet that renders the next moot?

fax8’s picture

replace
return node_title_list($result);
with
return node_title_list($result) . "Your text";

sfetcu’s picture

Hi,

When I try to display multiple lists for different taxonomy terms on a same page, it shows only one list. What do I have to change in the php code in order to display all the lists?

Thank you!

patrick_IRE’s picture

To post within a Theming Template page.tpl.php rather than a PHP Block:

<?php
$terms = "11";
$count = 6;
$sql = "SELECT distinct n.title, n.nid FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid in ($terms) AND n.status=1 ORDER BY n.created DESC";
$result = db_query_range(db_rewrite_sql($sql), 0, $count);
if ($result) {
   $terms = node_title_list($result);
}
else {
  $terms = t('No nodes available.');
}
print $terms;
?>
chronosinteractive’s picture

How can you do this and print the whole node? (Instead of just a list of the node titles).

And would you be able to theme the node that is outputted via it's node-type.tpl.php template file?

deadjohn’s picture

hello ... i am new on Drupal and i have some problems ...
i copy and paste this code into a block and i choice php code on filter but i get nothing!
what i am doing wrong? i just changed the terms to mine and nothing returns to my page...
any suggestion?

tonyletrung’s picture

I am a new drupal user! I know very litle
i would like a code to display Top 10 newest post (node, not term) anh top 10 post which is the most viewed! help me please! Thanks very much!