Search code examples
drupaldrupal-viewsdrupal-taxonomy

Taxonomy list with content counters


Using Drupal 7,

I have a content type: Movie,

It has a taxonomy of actors: Keanu Reeves, Christian Bale, Milla Jovovich, etc.

I'd like to build a view which lists the actors taxonomy with a count of content matching the taxonomy term, for example:

Keanu Reeves (4)
Christian Bale (8)
Milla Jovovich (2)

I'm not sure how to build this view,

I've set a view with use aggregation, filter: =published, content type=movie, lang=users language,

fields: COUNT(Content: Actors)

It doesn't seem to accomplish my goal, please help!


Solution

  • Couldn't find a way to implement via view, here's a custom block code to replace the view.

    $vid = 3; //vocabulary id
    $num_term = 8; //limit maximum terms
    
    $query = "SELECT tid, name, count
    FROM (
    SELECT td.tid AS tid, name, COUNT(td.tid) AS count
    FROM taxonomy_term_data AS td
    JOIN taxonomy_index AS tn
      ON td.tid = tn.tid
    JOIN node AS n
      ON n.nid = tn.nid
    WHERE td.vid = ". $vid ."
      AND n.status = 1
    GROUP BY td.tid
    ORDER BY count DESC
    LIMIT ". $num_term . "
    ) AS t
    ORDER BY name ASC";
    
    $result = db_query($query);
    foreach($result as $term) {
    if ($term->count > 0) {
      echo l($term->name, "taxonomy/term/$term->tid").' ('.$term->count.')'.'<br/>';
    }
    }
    
    ?>