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
If you just need the number to print in a custom module output a lighter version can be:
LikeLike
Thanks for your code. It’s a very straight forward way to get the number of nodes. =D
LikeLike
what is the file name where we need to do changes
LikeLike
Are u working with Views? if yes, you could make use of the Views PHP module and add your custom php code.
Drupal 7 – Add PHP field to view
LikeLike
not with the views, manual module
LikeLike
Then u could use the code anywhere in your custom module.
LikeLike
What would the code look like using it in views with the views php field?
LikeLike
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.
LikeLike
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)
LikeLike
You are welcome. =D
LikeLike
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!
LikeLike
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.
LikeLike
Very helpful, thank you! 😀
LikeLike
Good to know that this post could help. =)
LikeLike
~5 years later and this post helped me out of a bind. THANKS!
LikeLike