| 5 taxonomy.module | taxonomy_get_term_by_name($name) |
| 6 taxonomy.module | taxonomy_get_term_by_name($name) |
| 7 taxonomy.module | taxonomy_get_term_by_name($name) |
Try to map a string to an existing term, as for glossary use.
Provides a case-insensitive and trimmed mapping, to maximize the likelihood of a successful match.
Parameters
$name: Name of the term to search for.
Return value
An array of matching term objects.
5 functions call taxonomy_get_term_by_name()
File
- modules/
taxonomy/ taxonomy.module, line 1078 - Enables the organization of content into categories.
Code
<?php
function taxonomy_get_term_by_name($name) {
return taxonomy_term_load_multiple(array(), array('name' => trim($name)));
}
?> Login or register to post comments
Comments
term by vocabulary?
Unbelivable, that this function still has no parameter to filter the result by vid. Even though the they recommand to have the $options parameter deprecated for the inner function, you can use this for the vid filter.
function taxonomy_get_term_by_name_and_vocabulary($name, $vid) {return taxonomy_term_load_multiple(array(), array('name' => trim($name), 'vid' => $vid));
}
Should work.
Non-deprecated way to get tid from term name in a vocabulary
If you have the term name and want to find its term id in a particular vocabulary, this function could help. For e.g. since terms do not have machine names, they cannot be exported by features, unless their term ids match in the different dbs.
/**
* Helper function to dynamically get the tid from the term_name
*
* @param $term_name Term name
* @param $vocabulary_name Name of the vocabulary to search the term in
*
* @return Term id of the found term or else FALSE
*/
function _get_term_from_name($term_name, $vocabulary_name) {
if ($vocabulary = taxonomy_vocabulary_machine_name_load($vocabulary_name)) {
$tree = taxonomy_get_tree($vocabulary->vid);
foreach ($tree as $term) {
if ($term->name == $term_name) {
return $term->tid;
}
}
}
return FALSE;
}
Use EntityFieldQuery instead
Instead of loading and looping over the entire taxonomy tree which could be potentially large, it would be better to use an EntityFieldQuery here. E.g. searching for the term "Foo" in vocabulary 1:
$query = new EntityFieldQuery;$result = $query
->entityCondition('entity_type', 'taxonomy_term')
->propertyCondition('name', 'Foo')
->propertyCondition('vid', 1)
->execute();
output of this
Thanks It helps
output of this is
Array(
[taxonomy_term] => Array
(
[5] => stdClass Object
(
[tid] => 5
)
)
)
thanks foobar, that works!
thanks foobar, that works!