I have 3 content types. I have 3 vocabularies - one for each content type.

Recently I have created a script to save nodes programatically based on batch files clients send me. I am very happy with it, and it works fine.

However, I currently have to manually manage a separate file for a huge multidimensional array. Here is an excerpt from the file.

$query['baby_clothes']['category']['id'][] = 1;
$query['baby_clothes']['category']['id'][] = 2;
$query['baby_clothes']['category']['id'][] = 3;
$query['baby_clothes']['category']['id'][] = 4;
$query['feeding']['category']['id'][] = 26;
$query['feeding']['category']['id'][] = 28;

The first dimension represents the content type and the others the id. There is one entry per unique taxonomy term id in the array. Basically I loop through the IDs; extract the content type from key [0]; save accordingly.

As the taxonomy grows and changes this becomes very inefficient to manage, and i would like to create these arrays dynamically, but I am having trouble understanding the database relationships to create this part of the code.

Basically it would work like this: "Get an array of content types. For each content type get the taxonomy IDs for the one vocabulary that is associated with the content type. Dynamically create the array."

Or, how about this? Would this be easier? "Get a list of taxonomy IDs. Get the content type with which this term is accociated. Now I have my content type and my id - save node accordingly."

I can see the node_type and taxonomy_vocabulary in the mysql database, but I am not sure how they relate to each other, or how to relate them so I get the desired result. I would prefer option two, but any direction is appreciated as well as are alternate suggestions. Pseudocode is fine. Thanks!

1 Answer 1


Not to discourage database deep-diving, but if you're bootstrapped into the Drupal system with your system (i.e. programmatically creating nodes), then you don't have to necessarily understand how the DB works to get this working. Just use the functions in the node and taxonomy API to generate your array.

Specifically, lets say you wanted to try for option #1. You could use


to pull a list of all of your content types, and then iterate through that to generate a list of taxonomy terms for each vocabulary. Here is a function I've used before to that effect, from VicTheme.com

 * Helper function return all the taxonomy terms of a given node type
 * @param $type
 * The array of machine name of the content type that the function should look for taxonomy terms
 * the array format should be : array('machine_name');
 * @return
 *   An array of taxonomy terms containing tid => human name.
function vt_commerce_api_get_vocabulary($type = array()) {
  // break if there are no types specified
  if (empty($type) || !is_array($type)) {
    return FALSE;

  $output = array();
  foreach (field_info_fields() as $field) {
    if ($field['type'] == 'taxonomy_term_reference' && is_array($field['bundles']['node'])) {
      foreach ($field['bundles']['node'] as $content_type) {
        if (in_array($content_type, $type)) {
          foreach ($field['settings']['allowed_values'] as $value) {
             $output[$value['vocabulary']] = $value['vocabulary'];

  return $output;
  • Ah, this will work fine. the $field bundles node array and the ability to determine the type alone gives me enough info to develop either direction, and no db issues to deal with. great direction, thanks!
    – blue928
    Commented Sep 21, 2012 at 5:15

Your Answer

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

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