Community Documentation

Display latest X nodes from a taxonomy terms set

Last updated March 14, 2009. Created by fax8 on April 11, 2007.
Edited by hplc. 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

<?php
// 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.');
?>

Comments

Avoid Duplicate Entries

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";

Version 6.

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.

use node_load

something like

<?php
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

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

Why this PHP block snippet can't display the last node?

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

Solved the last node display problem

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!

Text after PHP Snippet?

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?

Michael Morisy
Phone: 1-857-488-3081 | AIM: MMorisy
GTalk: morisy@gmail.com | Twitter: @morisy

replace return

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

------------
www.varesano.net - Fabio Varesano Personal Homepage

Multiple lists

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!

To post within a page.tpl.php..

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;
?>

How can you do this and print

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?

Not working!??

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?

help me

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!

TônyLêTrung

Page status

Needs updating

Log in to edit this page

About this page

Drupal version
Drupal 6.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.