Programmatically manipulating taxonomy terms in Drupal

Programmatically inserting, editing and deleting a taxonomy term using drupal api is as easy as A,B,C. After scanning taxonomy_term_save() function, it looks like that if we pass an associated array as an argument to this function with proper keys we can make this happen very easily.

Inserting a taxonomy term:

<?php
  $term = array(
    'vid' => 5, // Voacabulary ID
    'name' => 'Drupal', // Term Name
    'synonyms' => 'Druplet', // (Optional) Synonym of this term
    'parent' => 11, // (Optional) Term ID of a parent term
    'relations' => array(15), // (Optional) Related Term IDs
  );

  taxonomy_save_term($term);
?>

Editing a taxonomy term:

<?php
  $term = array(
    'vid' => 5, // Voacabulary ID
    'tid' => 17, // If we add key 'tid' to this array then the function will update this term.
    'name' => 'Drupal', // Term Name
    'synonyms' => 'Druplet', // (Optional) Synonym of this term
    'parent' => 11, // (Optional) Term ID of a parent term
    'relations' => array(15), // (Optional) Related Term IDs
  );

  taxonomy_save_term($term);
?>

Deleteing a taxonomy term:

<?php
  $term = array(
    'tid' => 17, // If only key in this array is 'tid', function will take it as a deletion request.
  );

  taxonomy_save_term($term);
?>

If there are more than one synonyms then you can concatenate each synonym with '\r\n'. Following is a example of inserting three synonyms, "Druplet, Drup, Ultimate Drupal".

'synonyms' => "Druplet\r\nDrup\r\nUltimate Drupal", // These are three synonyms

If there are more than one parent then you can add each parent in an array, e.g.

'parent' => array(11, 10, 13),

You can nest as many parents as you want, e.g.

'parent' => array(11, array(9, 10), 13),

If there are more than one relations to this term then you can add each relation in an array i.e.

'relations' => array(15, 16, 17),

Comments

Great help

Thank you for this post, was looking for this for a while!

How to validate term name is not empty while creating term progr

How to validate term name is not empty while creating term programmatically. i.e. Inserting a taxonomy term:

 

  $term = array( 

    'vid' => 5, // Voacabulary ID 

    'name' => '', // Term Name is blank then also it will insert term 

    'synonyms' => 'Druplet', // (Optional) Synonym of this term 

    'parent' => 11, // (Optional) Term ID of a parent term 

    'relations' => array(15), // (Optional) Related Term IDs 

  ); 

  taxonomy_save_term($term); 

 

Excellent

I actually tried to link a term to a node using this taxonomy.module function. However, I’m not able to get the desired results. I even referred the api.drupal. It has not been of much help. http://poweredwebsite.com/ Kindly give more guidelines.

You helped me a lot

Thank you!

Hey, thanks for this! Just

Hey, thanks for this! Just what i was looking for. 1 question- once we've inserted the term into our taxonomy, how can we programatically add/link that term to a node??

First of all accept my

First of all accept my appology for replying late, coming towards your question; If you want to programatically link a term to a node then use taxonomy.module function taxonomy_node_save($node, $terms), please check api.drupal.org for the details about this function. If need further help please post a reply to this comment.