How to programmatically modify fields of taxonomy terms? In D7, how do I programmatically add/modify fields to taxonomy terms? They are clearly fieldable and in UI I can add and edit fields in them just like I do in nodes but it proved to be no easy task in the code. Say I have a vocabulary named Fruit with terms like Apples and Bananas. I've added a field to it named main_article_id, so all terms have Name, Description and Main Article Id. When I create a node named Apples that has a reference to the Fruit vocabulary (field_fruit) and Apples term is selected, the code should update Apples term and set its main_article_id field to $node-&gt;nid. Here is my code:

//hook_node_insert fires after new node has been inserted into db
function MYMODULE_node_insert($node)
{
    $taxonomy_term_id = $node-&gt;field_FRUIT[LANGUAGE_NONE][0]['value'];
    $taxonomy_term = taxonomy_term_load($value['tid']);
    if($taxonomy_term != FALSE)
    {
        //doesn't work
        $taxonomy_term-&gt;field_main_article_id[LANGUAGE_NONE][0]['value'] = $node-&gt;nid;
        taxonomy_term_save($taxonomy_term);
    }
}
The column name for a field isn't always value; in the case of taxonomy term references it's tid, and in the case of node/entity references it depends on what module you're using. 

For a field created using the References module the name is nid, and for one created using the Entity Reference module, it's target_id.

The following code should work a bit better:

//hook_node_insert fires after new node has been inserted into db
function MYMODULE_node_insert($node)
{
    $taxonomy_term_id = $node-&gt;field_FRUIT[LANGUAGE_NONE][0]['tid'];

    // $value['tid'] isn't defined anywhere in this function scope so I 
    // assume you meant to use $taxonomy_term_id here
    $taxonomy_term = taxonomy_term_load($taxonomy_term_id);
    if($taxonomy_term != FALSE)
    {
        $column_name = 'nid'; // Or 'target_id' if you're using entity reference            

        $taxonomy_term-&gt;field_main_article_id[LANGUAGE_NONE][0][$column_name] = $node-&gt;nid;
        taxonomy_term_save($taxonomy_term);
    }
}