Display (x) nodes from a multiple categories

Last modified: October 26, 2009 - 20:35

PLEASE NOTE! The following snippet is user submitted. Use at your own risk! For users who have setup drupal using an alternate database to the default (MYSQL), please note that the snippets may contain some database queries specific to MYSQL.

This php snippet displays nodes from a list of categories specified in the first line:

<?php
/**
* This php snippet displays nodes
* from a list of categories specified in the first line ($taxo_id)
*
* To increase/decrease the number of nodes listed
* change the $list_length value to suit.
*
* Works with drupal 4.6.x & 4.5.x
*
* Snippet submitted by Robert Garrigos (robertgarrigos)
* Updated by Sean Robertson (seanr)
*/
$taxo_id = "3,4,5"; /* comma seperated list */
$list_length = 1;
$sql = "SELECT * FROM {node} INNER JOIN {term_node} ON {node}.nid = {term_node}.nid WHERE {term_node}.tid in ($taxo_id) ORDER BY {node}.created DESC LIMIT $list_length";
$result = db_query($sql);
while (
$anode = db_fetch_object($result)) {
$output .= theme('node', $anode, $teaser = FALSE, $page = FALSE);
}
print
$output;
?>

this snippet picks nodes

vthirteen - November 28, 2005 - 17:50

this snippet picks nodes classified under either taxo_id = 3;
or taxo_id = 4; or taxo_id = 5;

i.e. = a broader selection (OR principle)

i am looking into a snippet picking nodes classified under both taxo_id = 3; and taxo_id = 4; and taxo_id = 5;

i.e. = a tighter selection (AND principle)

i'll keep searching (or i'll learn php myself....)

This does it

Thn - January 10, 2006 - 16:17

I ripped this right out of the taxonomy.module

<?php
$taxo_id
= "12,28"; /* comma seperated list */
$list_length = 3;

$joins = '';
$wheres = '';
$taxo_ar = explode(",", $taxo_id);
foreach (
$taxo_ar as $index => $tid) {
  
$joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid';
  
$wheres .= ' AND tn'. $index .'.tid =' . $tid;
}
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, n.created FROM {node} n '. $joins .' WHERE n.status = 1 '. $wheres .' ORDER BY n.sticky DESC, n.created DESC LIMIT ' . $list_length;

$result = db_query($sql);
while (
$anode = db_fetch_object($result)) {
$output .= theme('node', $anode, $teaser = TRUE, $page = FALSE);
}
print
$output;
?>

No nested Selects...

The Same but with Teasers

pnikosis - July 4, 2006 - 13:01

The same as above, but with author, teasers and specific node type ($charlength defines the teaser longitude in chars)

<?php
$taxo_id
= "1,18"; /* comma seperated list */
$list_length = 10;
$charlength="150";
$content_type = "story";

$joins = '';
$wheres = "AND n.type = '$content_type' ";
$taxo_ar = explode(",", $taxo_id);
foreach (
$taxo_ar as $index => $tid) {
 
$joins .= ' INNER JOIN {term_node} tn'. $index .' ON n.nid = tn'. $index .'.nid';
 
$wheres .= ' AND tn'. $index .'.tid =' . $tid;
}
$sql = 'SELECT DISTINCT(n.nid), n.sticky, n.title, nr.teaser, n.created, u.name FROM {node} n JOIN {node_revisions} nr ON (n.nid = nr.nid and n.vid = nr.vid) INNER JOIN {users} u ON n.uid = u.uid '. $joins .' WHERE n.status = 1 '. $wheres .' ORDER BY n.sticky DESC, n.created DESC LIMIT ' . $list_length;

$result = db_query($sql);
while (
$anode = db_fetch_object($result)) {
$output .= "<p>" . l($anode->title, "node/$anode->nid") . "<br>" . substr(strip_tags($anode->teaser), 0, $charlength) ."...<em> Submited by: <strong>" . $anode->name . "</strong></em></p>";
}
print
$output;
?>

This should do the trick.

gollyg - April 17, 2006 - 13:28

This should do the trick. The following code

  $myNodes = taxonomy_select_nodes(array(8,4),"and");
  print taxonomy_render_nodes($myNodes);

will render all nodes that belong to taxonomy 4 and 8.
If you want to theme the output specifically you may need to loop through the dataset returned ($myNodes).

 
 

Drupal is a registered trademark of Dries Buytaert.