24

In Drupal 6 I would go to the taxonomy section of my /admin and I'd be able to find the $vid (numerical) in the URL.

In Drupal 7 (no doubt due to the introduction of terms as entities) the URL is no longer as verbose (and some would say cleaner) as it now shows the machine name (bundle name?) of the vocabulary as it can be seen in admin/structure/taxonomy/my_vocabulary/edit.

My end-game is to use taxonomy_get_tree($vid, $parent, $max_depth, $load_entities) to load my vocabulary, and work with it in glorious and plentiful ways, but alas this function does not accept a machine_name but presumably prefers the numerical $vid.

I'll accept alternatives to load a full vocab tree (i.e. all terms and their relationships to each other), but I do think that this question should be answered directly for future generations to google.

1
  • just a guess but devel module? Apr 27, 2011 at 8:19

8 Answers 8

32

Actually you can use:

$vocab = taxonomy_vocabulary_machine_name_load('my_vocabulary');
$vid = $vocab->vid;
1
10

Following Manu, if you have command line Drush access, you could do:

drush php-eval '$tax=taxonomy_vocabulary_machine_name_load("main_site_structure");echo $tax->vid;'
1
  • woa, never expected a drush solution :) thanks for spicening (and widening) this question & answer
    – electblake
    Jun 5, 2011 at 0:41
1

I am using entity_load() to load my vocab object, and recieve it's $vid.

The trick is to pass in false for the 2nd parameters (which is $ids) and then reference the taxonomy_vocabulary table in your mysql database to see what you can pass in as conditions. I chose to use the machine_name as you can see below:

$ids = false;
$conditions = array('machine_name' => 'my_vocabulary');
$vocab = entity_load('taxonomy_vocabulary', $ids, $conditions);

If you know of a quicker/lighter way then please offer it here :)

2
  • 1
    Use EntityFieldQuery. $conditions are deprecated, and more limited. Apr 28, 2011 at 21:16
  • and so they are :) Care to submit a full answer? If not I guess I'll submit something here to complete/close this question.
    – electblake
    Apr 29, 2011 at 16:42
1

You can use manually examine the {taxonomy_vocabulary} table in the database, and then check the vid column.

1

In D7

If all you need is the vocabulary ID (vid) and you know the machine name, you can use:

$query = db_select('taxonomy_vocabulary', 'tv');
$query->fields('tv', array('vid'));
$query->condition('tv.machine_name', __VOCAB_MACHINE_NAME, '=');
$result = $query->execute();
$data = $result->fetchAssoc();
$vid = $data['vid'];

Small performance increase: ~0.0036489963531494 seconds to ~0.00030779838562012 seconds.

This, of course, could be tailored as needed. Just change the condition to what information you have.

0
1

If you inspect the "Edit Vocabulary" link the vid is in the ID attribute e.g. "edit-12-edit".

0

This is to load using entity field query as bojan mention:

$query = new entityFieldQuery();
$result = $query
  ->entityCondition('entity_type', 'taxonomy_vocabulary')
  ->propertyCondition('machine_name', 'my_vocabulary')
  ->execute();

if (empty($result['taxonomy_vocabulary'])) {
  return;
}

$vocabularies = taxonomy_vocabulary_load_multiple(array_keys($result['taxonomy_vocabulary']));
dpm($vocabularies);
0

This was the fastest way for me to see a list of all the vocabulary IDs via Drush:

  1. Connect to your database with the Drush command
    drush sqlc
  2. At the mysql prompt, type
    select * from taxonomy_vocabulary;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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