If you want to get vocabulary id by name in Drupal? Use below function to get vocabulary id by name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | /** * 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.
1 2 | $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.
If you like this question & answer and want to contribute, then write your question & answer and email to freewebmentor[@]gmail.com. Your question and answer will appear on FreeWebMentor.com and help other developers.