-1
  if($node->type=='student')
    {
        $nid = $node->nid;
        print_r("The node id is: ".$nid.'<br/>');
        $tid =db_query('SELECT tid FROM {taxonomy_index} WHERE nid = :nid', 
                  array(':nid' => $nid)
                )->fetchField();
        print_r('The tid is: '.$tid);exit(0);
    }

I tried but nothing is displayed...I just want to get the tid.

5
  • The tid is already there in the node object you don't need to fetch it form the table. Commented Oct 7, 2013 at 8:03
  • tid != nid its not the same Commented Oct 7, 2013 at 8:04
  • do you mean $node->tid??
    – Johan
    Commented Oct 7, 2013 at 8:04
  • 1
    install devel module, you can output dpm($node) and you can find something similar to this $node->yourfield_name['und'][0]['tid'] Commented Oct 7, 2013 at 8:07
  • possible duplicate of Get taxonomy term ID in node
    – abhiklpm
    Commented Jul 15, 2015 at 16:45

4 Answers 4

1

May be you can use field_get_items: It will return return the field items in the language they currently would be displayed.

// Pass the node object $node instead of $nid.
// Get the field name of taxonomy field from content type.
$term_id = field_get_items('node', $node, $field_name);

$term_id will be array containing the term ids.

1

As commented by Neok, you can get the term id mapped with your node directly from node object like

$node->field_example['und'][0]['tid'];

0
    <?php 
        $activities_name = "activities"; // suppose you created a vocabulary that is "activities" which is attach with a particular node.

//NOw I will fetch all the details for this vocabulary from database

        $activities_result = taxonomy_vocabulary_machine_name_load($activities_name);

        echo $activities_result->vid; // now print the attached vocabulary id 

now fetch all the terms id tose are associated with this vocabulary

        $activities_term_data = taxonomy_get_tree($activities_result->vid, $parent = 0, $max_depth = NULL, $load_entities = FALSE);

        foreach($activities_term_data as $k => $v)
        {
        echo $v->tid."=>".$v->name; // print the termid and term name.
        }

    //$v->tid will show the terms ids.

// by the help of code you can get the vocabulary id and associated terms ids ( actually its not clear to me what the user exactly want term id or vocabulary id)
         ?>
1
  • Welcome to Drupal Answers! Whilst this may theoretically answer the question, it would be preferable to include the essential explanation of the code you posted, how it answers the question and how it should be used. Now users can copy-paste it, but there is a risk some of them will not understand what they are doing, and it will not improve their knowledge and skill.
    – Mołot
    Commented Jul 21, 2014 at 10:56
0

Instead of using db_query simply use db_select as

$tid = db_select('taxonomy_index', 'taxonomy_index')
  ->fields('taxonomy_index', array('tid', 'tid'))
  ->condition('nid', $nid)
  ->execute()->fetchAllKeyed();

So executing this would generate an array containing the list of tags a content is tagged with.

Thanks,

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.