Previously, we talked about how to get the number of nodes of a specific term.
Drupal – Get Number of node of Taxonomy Term by Term ID
How about if i want to get the number of nodes under a specific parent term? Unfortunately, there is no such function. so i try to get that number by SQL. Here is the PHP code.
<?php $sql = "SELECT count(1) AS value "; $sql .= "FROM node nod, term_node ter, term_hierarchy teh "; $sql .= "WHERE nod.type = '<content_type>' "; $sql .= "AND nod.nid = ter.nid "; $sql .= "AND ter.tid = teh.tid "; $sql .= "AND teh.parent = <parent_term_id>"; $result = db_query($sql); $count = db_fetch_object($result); print "<p>$count->value</p>"; ?>
Done =)