Question

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!

Was it helpful?

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/>';
}
}

?>

OTHER TIPS

There should be way to generate the View you're looking for without having to do custom coding. See if http://eureka.ykyuen.info/2012/01/20/drupal-7-get-number-of-nodes-of-taxonomy-term-in-views/ or http://dev.nodeone.se/en/the-aggregation-setting-the-emmajane-episode helps you.

In Drupal 7, it is possible to do this with Views. No coding or additional modules are required. See How To Use Views Aggregator to Create Taxonomy Term Count Block? for the recipe.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top