Drupal 7 – Get number of nodes of Taxonomy Term in Views

About 2 months ago i published a post on how to get the number of nodes of a specific taxonomy term by term ID.
Drupal – Get number of nodes of Taxonomy Term by Term ID

Unfortunately, the taxonomy_term_count_nodes() no longer works in Drupal 7 but you can get the count by Views in D7.

1. Create a term view (A vocabulary called XXX is used in this example)

2. Add a new relationship Taxonomy term: Content using XXX

3. Turn on the Use aggregation under OTHER settings

4. Add Content: Nid as new field and set the Aggregation type to Count

Done =)

Reference: Rework Term Node Count for D7

16 thoughts on “Drupal 7 – Get number of nodes of Taxonomy Term in Views”

  1. If you just need the number to print in a custom module output a lighter version can be:

    function term_count_nodes($tid) {
      $nodes = taxonomy_select_nodes($tid, FALSE);
      return count($nodes);
    }
    

    Like

      1. I think you need to add the hidden tid field first and then a php field. In the PHP field, you should be able to get the tid for each row.

        The PHP field should look like this i think.

        $nodes = taxonomy_select_nodes($row->tid, FALSE);
        print count($nodes);
        

        Like

  2. Thanks! Worked beautifully! I wanted the advanced user view to show how many posts were tied to a particular user and ended up having to add the node relationship to get it to work. (for anyone else looking to get a node count for users)

    Like

  3. Doesn’t work at all. Always shows “0” under tax terms and will only show “0” (or anything for that matter) if “Require this relationship” is set to false.

    Tried, re-tried, checked and re-checked this tutorial. Easier to just write a custom query to do it.

    Yaaaaaay Drupal!

    Like

    1. I just try again and and and “Require this relationship” is set to false. It works without problem. Here is the view settings for your reference.

      drupal7-term-count

      Like

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.