Drupal – Get the Number of nodes under a Parent Term

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.

	$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 =)


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 )

Connecting to %s

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