Finds all children of a term ID.
$tid: A taxonomy term ID.
$vid: An optional vocabulary ID to restrict the child search.
An array of term objects that are the children of the term $tid, or an empty array when no children exist.
function taxonomy_term_load_children($tid, $vid = NULL) {
$children =& drupal_static(__FUNCTION__, array());
if ($tid && !isset($children[$tid])) {
$query = db_select('taxonomy_term_data', 't');
$query
->join('taxonomy_term_hierarchy', 'h', 'h.tid = t.tid');
$query
->addField('t', 'tid');
$query
->condition('h.parent', $tid);
if ($vid) {
$query
->condition('t.vid', $vid);
}
$query
->addTag('term_access');
$query
->orderBy('t.weight');
$query
->orderBy('t.name');
$tids = $query
->execute()
->fetchCol();
$children[$tid] = taxonomy_term_load_multiple($tids);
}
return isset($children[$tid]) ? $children[$tid] : array();
}