3

When you tag a content type with a taxonomy term, if you click on the term itself, Drupal will display a list of content tagged with that term. I have reviewed taxonomy_select_nodes(), but I wasn't able to get a working piece of code. I'm not sure this is the correct function to use when looking for the node IDs of the nodes tagged with a taxonomy term.

How do you retrieve the IDs of the nodes using a specific taxonomy term?

2 Answers 2

2

EntityFieldQuery to the rescue:

$query = new EntityFieldQuery;
$query->entityCondition('entity_type', 'node')
  ->fieldCondition('field_name_of_term_reference_field', 'tid', $term_id);

$result = $query->execute();
if ($result) {
  $nids = array_keys($result['node']);
}
4
  • Thanks @Clive. This should work within a function of a module right? (pardon to noobness) Commented Jul 10, 2012 at 14:53
  • 1
    Yep, it'll work in a function in a module file, theme file, and even template file if you want it to (although please don't put it in a template file, your cache will never forgive you!)
    – Clive
    Commented Jul 10, 2012 at 14:58
  • It blew up! lol. Recoverable fatal error: Object of class stdClass could not be converted to string in DatabaseStatementBase->execute() (line 2136 of /database/database.inc). =[ Commented Jul 10, 2012 at 15:05
  • What are you passing as $term_id? If it's the taxonomy term object change it to $term_object->tid, that should fix it
    – Clive
    Commented Jul 10, 2012 at 15:07
1

taxonomy_select_nodes() works only if the field storage is the default one (field_sql_storage); with other field storages, or when the "taxonomy_maintain_index_table" Drupal variable is not set to TRUE, the function doesn't return any value.

In the first case, the "taxonomy_index" table used by the function will not be populated; in the second case, the function returns an empty array.

  if (!variable_get('taxonomy_maintain_index_table', TRUE)) {
    return array();
  }

The only way to get the list of nodes using a specific taxonomy term is using the EntityFieldQuery class, taking in consideration the following notes. I assume $query is an instance of the EntityFieldQuery class requiring a list of nodes; this means the following lines are being used.

$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node');
  • If you are interested in getting all the published nodes, you should add the following line:

    $query->propertyCondition('status', 1);
    

Differently, you will get also the nodes that are unpublished.

  • The EntityFieldQuery class verifies the currently logged in user has access to the fields being checked by the query. To avoid this, if it is not desired, you should add the following line:

    $query->addMetaData('account', user_load(1));
    

For more information about how to use the EntityFieldQuery class see How to use EntityFieldQuery.

0

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.