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

About these ads

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

  1. mwesthof

    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

    Reply
      1. ykyuen Post author

        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. Phil

    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

    Reply
  3. Nope

    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

    Reply
    1. ykyuen Post author

      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

      Reply

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s