get vocabulary id by name
Solution 1
I have a function for this, well almost..
/**
* This function will return a vocabulary object which matches the
* given name. Will return null if no such vocabulary exists.
*
* @param String $vocabulary_name
* This is the name of the section which is required
* @return Object
* This is the vocabulary object with the name
* or null if no such vocabulary exists
*/
function mymodule_get_vocabulary_by_name($vocabulary_name) {
$vocabs = taxonomy_get_vocabularies(NULL);
foreach ($vocabs as $vocab_object) {
if ($vocab_object->name == $vocabulary_name) {
return $vocab_object;
}
}
return NULL;
}
If you want the vid just get the vid property of the returned object and.
$vocab_object = mymodule_get_vocabulary_by_name("listing");
$my_vid = $vocab_object->vid;
Henriks point about storing it in a variable is very valid as the above code you won't want to be running on every request.
Edit
Also worth noting that in Drupal 7 you can use taxonomy_vocabulary_get_names() which makes this a little easier.
Solution 2
For Drupal 7 if you know the vocabulary machine name this is the way:
$vid = taxonomy_vocabulary_machine_name_load('your_vocabulary_name')->vid;
If you know only the Real name of vocabulary, you can use this function:
function _get_vocabulary_by_name($vocabulary_name) {
// Get vocabulary by vocabulary name.
$query = db_select('taxonomy_vocabulary', 'tv');
$query->fields('tv', [
'machine_name',
'vid',
]);
$query->condition('tv.name', $vocabulary_name, '=');
$vocabulary = $query->execute()->fetchObject();
return $vocabulary;
}
Solution 3
There is no built in function for this, afaik. You can roll your own by calling taxonomy_get_vocabularies()
and search for your name in the resulting array, but this will do a database request on every call.
If you have a vocabulary that you often use from code, it might be easier/more effective to store the vid in a Drupal variable via variable_set()
once and get it back via variable_get()
(Many modules that create a vocabulary on install do it this way).
Edit: here is some sample code to do this on module install.
function mymodule_install() {
$ret = array();
$vocabulary = array(
'name' => t('myvocab'),
'multiple' => '1',
'required' => '0',
'hierarchy' => '1',
'relations' => '0',
'module' => 'mymodule',
'nodes' => array('article' => 1),
);
taxonomy_save_vocabulary($vocabulary);
$vid = $vocabulary['vid'];
variable_set('mymodule_myvocab', $vid);
return $ret
}
Solution 4
Should help.
function _my_module_vid($name) {
$names = taxonomy_vocabulary_get_names();
return $names[$name]->vid;
}
Milan
Updated on June 04, 2022Comments
-
Milan almost 2 years
I can retrieve a vocabulary id directly from DB, but is there a built in function for this?
for example:
i have a vocabulary called "listing", i need that built in function takes "listing" as function argument, and return a vid.
i am using drupal 6
-
hasni over 14 yearsHenrik, I hope you don't mind, but as I had some code to hadn I put a sample in for storing a vocab on install.
-
Aditya M P about 12 yearsI was about to suggest taxonomy_get_vocabularies and saw your note regarding another DB call on every request... you're right.
-
AKS over 11 yearsThis should work, but what about an isset check and returning FALSE if there is no vocab with that name ?
-
mate64 over 10 yearsD7 users please notice: Use
$vocab_object->machine_name
instead of->name
! -
Sswater Shi about 10 yearsmisspelling, should be
taxonomy_vocabulary_save
-
Henrik Opel about 10 years@SswaterShi: This answer and code where aimed at Drupal 6, where it was taxonomy_save_vocabulary.