Display a list of categories and number of posts in each

Last modified: September 1, 2009 - 00:57

Create a block with a list of categories and post count next to each, like in this example:
* Illustrator (35)
* InDesign (44)
* Photoshop (178)
* Quark XPress (4)

<?php
 
if (user_access('access content')) {
   
$result = db_query("SELECT term_data.tid, term_data.name, COUNT(*) AS count FROM {vocabulary_node_types} INNER JOIN  {term_data} USING (vid) INNER JOIN {term_node} USING (tid) INNER JOIN {node} USING (nid) WHERE node.status = 1 and vocabulary_node_types.type = 'blog' GROUP BY term_data.tid, term_data.name ORDER BY term_data.name");
   
$items = array();
    while (
$category = db_fetch_object($result)) {
     
$items[] = l($category->name .' ('. $category->count .')', 'taxonomy/term/'. $category->tid);
    }

    return
theme('item_list', $items);
  }
?>

Drupal 6

barrandow - June 5, 2009 - 19:33

In Drupal 6 just retype "blog" to "story":
...vocabulary_node_types.type = 'story'...

It did not work in my case.

chillz - June 30, 2009 - 13:12

It did not work in my case. I tested in in Drupal 6.12 and got the following error:

user warning: Unknown column 'term_data.tid' in 'field list' query: SELECT term_data.tid, term_data.name, COUNT(*) AS count FROM drupal_vocabulary_node_types INNER JOIN drupal_term_data USING (vid) INNER JOIN drupal_term_node USING (tid) INNER JOIN drupal_node USING (nid) WHERE node.status = 1 and vocabulary_node_types.type = 'story' GROUP BY term_data.tid, term_data.name ORDER BY term_data.name in C:\srv\apache\htdocs\drupal\includes\common.inc(1645) : eval()'d code on line 3.

Any idea why this is happening?

This worked for me in drupal

westwesterson - September 1, 2009 - 00:56

This worked for me in drupal 6.13, make sure that you have php input filter enabled.

Also I tweaked this script slightly so that it would list terms by the count of taxonomy items. And limited to 25 terms.

<?php
 
if (user_access('access content')) {
   
$result = db_query("SELECT term_data.tid, term_data.name, COUNT(*) AS count FROM {vocabulary_node_types} INNER JOIN  {term_data} USING (vid) INNER JOIN {term_node} USING (tid) INNER JOIN {node} USING (nid) WHERE node.status = 1 and vocabulary_node_types.type = 'story' GROUP BY term_data.tid, term_data.name ORDER BY count desc LIMIT 25");
   
$items = array();
    while (
$category = db_fetch_object($result)) {
     
$items[] = l($category->name .' ('. $category->count .')', 'taxonomy/term/'. $category->tid);
    }

    return
theme('item_list', $items);
  }
?>

Catagories should now show up as:
* Photoshop (178)
* InDesign (44)
* Illustrator (35)
* Quark XPress (4)

There's a Taxonomy block

espirates - October 3, 2009 - 07:33

There's a Taxonomy block module that does this perfectly

It works nice for me on

etcetera9 - October 25, 2009 - 12:24

It works nice for me on Drupal 6.14. But I was wondering instead of terms, can I show number of nodes in a whole vocabulary?

I mean instead of:
Austria(5)
Angola(2)
Australia(8)

I need:
Countries(15)

I have just found a solution

etcetera9 - October 25, 2009 - 12:32

I have just found a solution if anyone needs:

<?php

  $output
= '';
  foreach(
taxonomy_get_vocabularies() as $vocab) {
   
$term_count = db_result(db_query("SELECT count(*) FROM {term_data} WHERE vid = %d", $vocab->vid));
   
$output .= '<h4>' . $vocab->name . '</h4><p>Total ' . $term_count . ' terms.<br/>';

  print
$output;
?>

 
 

Drupal is a registered trademark of Dries Buytaert.