Hi,

I would like to get a list of child terms from a parent term based on its name rather than id. Here is the info for the id solution:
http://api.drupal.org/api/function/taxonomy_get_children

<?php
function taxonomy_get_children($tid, $vid = 0, $key = 'tid') {
  if (
$vid) {
   
$result = db_query(db_rewrite_sql('SELECT t.* FROM {term_data} t INNER JOIN {term_hierarchy} h ON h.tid = t.tid WHERE t.vid = %d AND h.parent = %d ORDER BY weight, name', 't', 'tid'), $vid, $tid);
  }
  else {
   
$result = db_query(db_rewrite_sql('SELECT t.* FROM {term_data} t INNER JOIN {term_hierarchy} h ON h.tid = t.tid WHERE parent = %d ORDER BY weight, name', 't', 'tid'), $tid);
  }
 
$children = array();
  while (
$term = db_fetch_object($result)) {
   
$children[$term->$key] = $term;
  }
  return
$children;
}
?>

http://drupal.org/node/84517

Jacob Persson posted a reply showing how to do it by id....

<?php
//yourtid = the tid of the term
//yourvid = the vid of the vocab
$yourtid = 1;
$yourvid = 1;
$children = taxonomy_get_children($yourtid, $yourvid);
foreach (
$children as $term) {
       
$items[] = $term->name;
}
print
theme('item_list', $items);
?>

This worked well but I could not adapt it to work for the term name. I have checked in the term_data table and the field I wish to query is called 'name'.

I am not a programmer but tried the following:

//yourtname = the term name
//yourvid = the vid of the vocab
$yourtname = "termname";
$yourvid = 1;
$yourkey = "name";
$children = taxonomy_get_children($yourtid, $yourvid, $yourkey);
foreach ($children as $term) {
        $items[] = $term->name;
}
print theme('item_list', $items);

Does anyone have any suggestions?

Thanks

Joe

Comments

In the interest of providing insight for anyone else wanting this functionality, I used the following solution to get a term by the parent's name:

<?php
if(isset($data['node'])) {
  
$tags = taxonomy_get_tree('1');
   foreach(
$tags as $key => $tag_data) {
/** Put the parent term name where it says 'X' **/
     
if($tag_data->name == 'X') {
        
$children = taxonomy_get_children($tag_data->tid, 1);
      }
   }
  
$field_data = field_view_field('node', $data['node'], 'field_tags');
   foreach(
$field_data['#items'] as $key => $term_data) {
      foreach (
$children as $term) {
         if(
$term_data['taxonomy_term']->name == $term->name) {
            return
strtolower($term_data['taxonomy_term']->name) . '/';
         }
      }
   }
}
?>

Genius is one percent inspiration and ninety-nine percent perspiration.
Thomas A. Edison