4.6.x taxonomy.module taxonomy_get_children($tid, $vid = 0, $key = 'tid')
4.7.x taxonomy.module taxonomy_get_children($tid, $vid = 0, $key = 'tid')
5.x taxonomy.module taxonomy_get_children($tid, $vid = 0, $key = 'tid')
6.x taxonomy.module taxonomy_get_children($tid, $vid = 0, $key = 'tid')
7.x 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()
TaxonomyQueryAlterTestCase::testTaxonomyQueryAlter in modules/taxonomy/taxonomy.test
Tests that appropriate tags are added when querying the database.
TaxonomyTermTestCase::testTaxonomyTermHierarchy in modules/taxonomy/taxonomy.test
Test terms in a single and multiple hierarchy.
1 string reference to 'taxonomy_get_children'
taxonomy_terms_static_reset in modules/taxonomy/taxonomy.module
Clear all static cache variables for terms.

File

modules/taxonomy/taxonomy.module, line 1075
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('taxonomy_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

BartK’s picture

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);

Wim Leers’s picture

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

ebeyrent’s picture

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

lmeurs’s picture

Same here, $tid=0 should return the root terms...

lmeurs’s picture

Same here, $tid=0 should return the root terms... (sorry for the double posting, it looked like the form could not submit)

Jaypan’s picture

Agreed

dscutaru’s picture

taxonomy_get_children('zero')
works too, just be careful with cache

intu.cz’s picture

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.

ianthomas_uk’s picture

Am I correct in thinking that no two taxonomy terms on the same Drupal install can have the same $tid, even if they are in different vocabularies, and a parent cannot have children in a different vocabulary?

If so, what is the benefit of passing $vid? I notice it's not on similar functions such as taxonomy_term_load().

N1ghteyes’s picture

tid is unique, so no.

UnsettlingTrend’s picture

Is it at all possible to place a taxonomy term from one vocab under one from a different vocab? Then you would be limiting the children only to the vocab you specify? (I really don't think you can, but that's all I can come up with.)

rooby’s picture

You might not know what vocab your tid belongs to, so if you only want to deal with vocab 1 and have some random tid, you pass the tid and the vid in and if the vid doesn't match the tid you don't get the children and if it does you do.

It's basically a filter.