I have a node that is tagged with a taxonomy term which is part of a hierarchy of terms such as "state >> county."

As the user has selected one of the "county" tags, how can I get the name of the parent term?

2 Answers 2


You can query the database yourself, or you can use the API function: taxonomy_get_parents

Since you might have several parents, it might be easier to just query the database yourself:

$tid = ? // the tid of the county term
$parent_tid = db_result(db_query("SELECT parent FROM {term_hierarchy} WHERE tid = %d;",
  array(':tid' => $tid)
$parent_term = taxonomy_get_term($parent_tid);

Update - Why I think the above query is smart in Drupal 6

Some (kiamlaluno) would argue that it's wrong to use the above argument for db_query in Drupal 6

In Drupal 6 You can use db_query in three ways:

$query = db_query($sql, $arg1, $arg2, $arg3);
$query = db_query($sql, array($arg1, $arg2, $arg3));
$query = db_query($sql, array(':arg1' => $arg1, ':arg2' => $arg2, ':arg3' => $arg3));

All of the above will return the same thing, so there are no functional difference using one over the over. Why then you might ask, do I think it's clever to use the last one?

If you look at how Drupal 7 does it you will see:

$query = db_query($sql, array(':arg1' => $arg1, ':arg2' => $arg2, ':arg3' => $arg3));

Drupal 7 requires the query arguments formatted in a special way. What you can't see in this example is that the SQL needs to be changed as well. Instead of using placeholders like %d you need to use place holders like :arg1. So while the latter of the 3 Drupal 6 ways of using db_query is the most verbose, it will save you a lot of work, should you need to upgrade your Drupal 6 module to Drupal 7. Which might happen in a year, where you don't remember what you were doing and why.

But at the end of the day, it's just personal preference, as either way will work just fine.

  • 1
    That sql query is wrong, what table do I select from?
    – Arosboro
    Jul 9, 2011 at 21:04
  • I found it, it needs to be SELECT parent FROM taxonomy_hierarchy WHERE tid = %d;
    – Arosboro
    Jul 9, 2011 at 21:12
  • @Arosboro Sorry was a bit to quick, forgot to include the table
    – googletorp
    Jul 9, 2011 at 21:31
  • The argument passed to db_query() is also wrong.
    – apaderno
    Jul 9, 2011 at 23:54
  • @kiamlaluno care to explain?
    – googletorp
    Jul 10, 2011 at 8:23

taxonomy_get_parents() returns all the parent terms for the taxonomy term with the ID passed as argument.
The value returned from the function is an array having the following structure:

  'term id' => [taxonomy term object]

The taxonomy term object contains a property for each of the fields contained in the "term_data" table.

The code used from the function is essentially the following one.

$result = db_query(db_rewrite_sql('SELECT t.tid, t.* FROM {term_data} t INNER JOIN {term_hierarchy} h ON h.parent = t.tid WHERE h.tid = %d ORDER BY weight, name', 't', 'tid'), $tid);
$parents = array();
while ($parent = db_fetch_object($result)) {
  $parents[$parent->$key] = $parent;

As you see, it doesn't return the parent terms of the parent terms of the one passed as argument. For that you need taxonomy_get_parents_all(), which executes the following code.

  if ($tid) {
    $parents[] = taxonomy_get_term($tid);
    $n = 0;
    while ($parent = taxonomy_get_parents($parents[$n]->tid)) {
      $parents = array_merge($parents, $parent);

The function returns all the parent terms in the hierarchy.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.