taxonomy_get_children

5 taxonomy.module taxonomy_get_children($tid, $vid = 0, $key = 'tid')
6 taxonomy.module taxonomy_get_children($tid, $vid = 0, $key = 'tid')
7 taxonomy.module taxonomy_get_children($tid, $vid = 0)

Finds all children of a term ID.

Parameters

$tid: A taxonomy term ID.

$vid: An optional vocabulary ID to restrict the child search.

Return value

An array of term objects that are the children of the term $tid, or an empty array when no children exist.

2 calls to taxonomy_get_children()

1 string reference to 'taxonomy_get_children'

File

modules/taxonomy/taxonomy.module, line 955
Enables the organization of content into categories.

Code

function taxonomy_get_children($tid, $vid = 0) {
  $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();
}

Comments

How to get top level items

I'm of the opinion that it would be nice if this function returned top level items if you pass it a parent tid of 0. However, that doesn't currently work with this function.

To get the top level, you have to call taxonomy_get_tree and tell it to return a maximum depth of 1.

taxonomy_get_tree($vid, 0, 1);

Regression

Indeed. In my opinion, this is even a regression. It used to work that way in Drupal 5 & 6.

Agreed. I just came across

Agreed. I just came across this while porting a theme to D7.

taxonomy_get_children_all

http://drupal.org/node/381952#comment-4490228 has a good function to get all children in a not too complex array, so that all children are accessible without the need to flatten the array.

Login or register to post comments