Display a list of term titles(x) from a specific vocabulary with the number of nodes in each term
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 can either pass the query through db_rewrite_sql or use node_load on selected nids.
Note; the information leak consists of node counts & taxonomy terms.
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.
<?php
/**
* This php snippet displays a themed summary list of vocabulary term titles(x) from a specific category
* Where (x) is the number of nodes in each term.
* The Snippet ignores terms with no nodes.
*
* Tested and works with Drupal 4.6
* To change the vocabulary references, change the $vocabulary_id value to suit.
*/
$vocabulary_id = 1;
$result = db_query("SELECT d.tid, d.name, MAX(n.created) AS updated, COUNT(*) AS count FROM {term_data} d INNER JOIN {term_node} USING (tid) INNER JOIN {node} n USING (nid) WHERE d.vid = $vocabulary_id AND n.status = 1 GROUP BY d.tid, d.name ORDER BY updated DESC, d.name");
$items = array();
while ($category = db_fetch_object($result)) {
$items[] = l($category->name .' ('. $category->count .')', 'taxonomy/term/'. $category->tid) .'<br />'. t('%time ago', array('%time' => format_interval(time() - $category->updated)));
}
print theme('item_list', $items);
?>
Showing terms for selected node types
To limit the list to count items of a selected node type, change the $node_type variable in the code below:
<?php$node_type = "flexinode-x";
$vocabulary_id = y;
$result = db_query("SELECT d.tid, d.name, MAX(n.created) AS updated, COUNT(*) AS count FROM {term_data} d INNER JOIN {term_node} USING (tid) INNER JOIN {node} n USING (nid) WHERE d.vid = $vocabulary_id AND n.status = 1 AND n.type = '$node_type' GROUP BY d.tid, d.name ORDER BY d.weight");
$items = array();
while ($category = db_fetch_object($result)) {
$items[] = l($category->name .' ('. $category->count .')', 'taxonomy/term/'. $category->tid) .
t(' %time ago', array('%time' => format_interval(time() - $category->updated)));
}
print theme('item_list', $items);
?>