Hi,
Thanks for your answer. This is a nice function indeed, it's just a pity we can't precise a vocabulary in it... but i'll still use it, so thanks again!
Seems silly that it's so hard to target a taxonomy term in a specific vocabulary. Here's the function I wrote to solve this problem. It's pretty flexible, but has only been tested on Drupal 6:
/**
* Function to get Drupal taxonomy term id for a given vocabulary. Both term and
* vocabulary may be passed either as a numeric id or the name as a string. Name
* matches are case insensitive and strip leading and trailing spaces.
*
* @param mixed $term The ID or name of desired term
* @param mixed $vocab The ID or name of desired vocabulary
* @return mixed Returns the term ID, or NULL if term is not found
*/
function get_taxonomy_term_by_vocab( $term, $vocab ){
$sql = "";
// if both are ids
if( is_numeric( $vocab ) && is_numeric( $term ) )
$sql = "SELECT tid FROM term_data WHERE vid=$vocab AND tid=$term";
// if vocab is id and term is name
if( is_numeric( $vocab ) && !is_numeric( $term ) )
$sql = "SELECT tid FROM term_data WHERE vid=$vocab AND TRIM(BOTH ' ' FROM `name`) LIKE '$term'";
// if vocab is name and term is id
if( !is_numeric( $vocab ) && is_numeric( $term ) ){
$sql = "SELECT term_data.tid FROM term_data INNER JOIN vocabulary ON term_data.vid = vocabulary.vid ";
$sql .= "WHERE TRIM(BOTH ' ' FROM vocabulary.`name`) LIKE '$vocab' AND ";
$sql .= "term_data.tid = $term";
}
// if both are names
if( !is_numeric( $vocab ) && !is_numeric( $term ) ){
$sql = "SELECT term_data.tid FROM term_data INNER JOIN vocabulary ON term_data.vid = vocabulary.vid ";
$sql .= "WHERE TRIM(BOTH ' ' FROM vocabulary.`name`) LIKE '$vocab' AND ";
$sql .= "TRIM(BOTH ' ' FROM term_data.`name`) LIKE '$term'";
}
// do query
$tid_rsrc = db_query($sql);
$row = db_fetch_array($tid_rsrc, MYSQL_ASSOC);
return $row['tid'];
}
After you get your results from the taxonomy_get_term_by_name you should have a term id. This would mean that you can now use taxonomy_get_term. This function takes a term id as its parameter. You will then be returned a full term object which will have the vocabulary id (taxonomy) that it is located in.
Comments
taxonomy_get_term_by_name
You should be able to use the following function provided by the Drupal API. taxonomy_get_term_by_name
Hi, Thanks for your answer.
Hi,
Thanks for your answer. This is a nice function indeed, it's just a pity we can't precise a vocabulary in it... but i'll still use it, so thanks again!
Seems silly that it's so hard
Seems silly that it's so hard to target a taxonomy term in a specific vocabulary. Here's the function I wrote to solve this problem. It's pretty flexible, but has only been tested on Drupal 6:
After you get your results
After you get your results from the taxonomy_get_term_by_name you should have a term id. This would mean that you can now use taxonomy_get_term. This function takes a term id as its parameter. You will then be returned a full term object which will have the vocabulary id (taxonomy) that it is located in.