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:
function term_count_nodes($tid) { $nodes = taxonomy_select_nodes($tid, FALSE); return count($nodes); }Thanks for your code. It’s a very straight forward way to get the number of nodes. =D
what is the file name where we need to do changes
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
not with the views, manual module
Then u could use the code anywhere in your custom module.
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)
You are welcome. =D
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!
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.
Very helpful, thank you!
Good to know that this post could help. =)