Display (x) nodes which is associated with all specified terms

Last modified: May 17, 2009 - 00:39

<?php
/**
* Creates a list of node titles which is associated with all specified terms
*
* To add more terms, simply add  more terms to the IN clause and change the  .
* term_count variable
* To change the number of node titles listed, simply edit the $list_no number.
*
* This snippet works with Drupal 4.6.x
* Tested with site <a href="http://www.supplychain-logistics.com" title="http://www.supplychain-logistics.com" rel="nofollow">http://www.supplychain-logistics.com</a> for page
* <a href="http://supplychain-logistics.com/civicspace/node/201
" title="http://supplychain-logistics.com/civicspace/node/201
" rel="nofollow">http://supplychain-logistics.com/civicspace/node/201
</a> *
*/
$taxo_id = 6;
$list_no = 10;
$term1 = 'sourcing' ;
$term2 = 'supplychain' ;
$term3 = 'software solution' ;
$term_count = 3;
$sql = "SELECT n.title , n.nid from {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid INNER JOIN {term_data} d ON d.tid = tn.tid WHERE d.name IN('$term1','$term2','$term3') GROUP BY n.title HAVING COUNT(*) = $term_count";
$output .= "<ul>";
$result = db_query($sql);
while (
$anode = db_fetch_object($result)) {
$output .= "<li>".l($anode->title, "node/$anode->nid")."</li>";
}
$output .= "</ul>";
print
$output;
?>

Nice Breaking

A slightly different version that displays the node body a bit more cleanly (on phrases or other markers, like node_teaser does).

<?php
/**
* Creates a list of node titles selected from multiple category terms
* in descending chronological order (most recent first).
* Titles link to full node.
*
* To change category terms to select from,
* edit the $taxd_id numbers, retaining the "" marks.
*
* To change the number of node titles, edit the $list_no number.
*
* This snippet is tested with Drupal 4.6.x
*
*/

$taxo_id = "61,72,74,75,76,77,78,79,80,81,82,83,84";
$list_no =3;
$query = "SELECT DISTINCT(n.nid), n.title, n.body, n.created FROM {node} n INNER JOIN {term_node} tn ON n.nid = tn.nid WHERE tn.tid in ($taxo_id) AND n.status = 1 ORDER BY n.created DESC LIMIT $list_no";
$sql = db_rewrite_sql($query);
$result = db_query($sql);
$items = array();

$breakpoints = array('. ' => 1, '! ' => 1, '? ' => 1, '。' => 1, '</p>' => 4, '<br />' => 0, '<br>' => 0, "\n" => 0);
while (
$anode = db_fetch_object($result)) {
 
$body = preg_replace('/\[img_assist\|[^]]\]/', '', preg_replace('/<img[^>]*>/', '', $anode->body));
 
// taken from node.module:node_teaser()
 
foreach ($breakpoints as $point => $charnum) {
    if (
$length = strpos($body, $point, 100)) {
     
$body = substr($body, 0, $length + $charnum);
      break;
    }
  }

  if (
$body != $anode->body) $body .= '...';
 
$items[]= l($anode->title, "node/$anode->nid") . '<br />' . $body;
}

if(
count($items)) {
echo
theme('item_list',$items);
}
?>

 
 

Drupal is a registered trademark of Dries Buytaert.