Last updated 7 April 2014. Created on 6 April 2012.
Edited by Alan D., Gold, adityamenon. Log in to edit this page.

This belongs inside your module code.

You can get a collection of nodes matching particular parameters by using EntityFieldQuery, instead of writing an SQL statement. I had a hard time getting all the syntax together, so here are a couple of simple samples:

// We use the machine name to ensure we have the proper vocab
// that is not fixed to a specific vid on a installation.

// Define this outside of the two conditions to avoid PHP notices.
$term_options_list = array();      
if ($range_vocab = taxonomy_vocabulary_machine_name_load('range')) {
  $efq = new EntityFieldQuery();
  $result = $efq->entityCondition('entity_type', 'taxonomy_term')
    ->propertyCondition('vid', $range_vocab->vid)
  if (!empty($result['taxonomy_term'])) {
    // The results contain an array of entity stubs keyed by {term}.tid.
    // The stub only has {term}.tid and vocabulary_machine_name.
    $first_term_stub = current($result['taxonomy_term']);
    $first_term_tid = key($result['taxonomy_term']);

    // To load all terms.
    $terms = taxonomy_term_load_multiple(array_keys($result['taxonomy_term']));
    // To generate an options list.
    foreach ($terms as $term) {
      // To hook into i18n and everything else, use entity_label().
      $term_options_list[$term->tid] = entity_label('taxonomy_term', $term);
      // Single language, no term label alters.
      $term_options_list[$term->tid] = $term->name;      
// IMPORTANT. This was for a select list that expects raw values.
// If ANYTHING else is required, escape the term names, either here
// or where the name is used.
$safe_term_options_list = array_map('check_plain', $term_options_list);

  //get a list of nodes that match your criteria
  $nodeQuery = new EntityFieldQuery();
  $entities = $nodeQuery->entityCondition('entity_type', 'node')
    // change 'food_menu_item' to target content_type
    ->entityCondition('bundle', 'food_menu_item') 
    // get only nodes that are 'published'
    ->propertyCondition('status', 1) 
    // replace field_food_menu with field_TAXONOMY_NAME 
    // replace 2 with the taxonomy ID (tid) you're wanting
    ->fieldCondition('field_food_menu', 'tid', 2);

  // If multiple tids are required use this code in a loop with 
  // one tid per fieldCondition()
  $restriction_tids = array(13,15);
  foreach ($restriction_tids as $r_tid){
    $entities->fieldCondition('field_food_restrictions', 'tid', $r_tid);

  // If you want to check for at least one of a list of tids replace 2 
  // with an array.  e.g. array(2,4,5)
  $which_menu_tids = array(23,24);
  $entities->fieldCondition('field_which_menu', 'tid', $which_menu_tids, 'IN');

  // Gets an array keyed by entity type, then entity id that match 
  // your criteria. In this case :
  // field_food_menu term ID 2 
  // AND field_food_restrictions term ID 13
  // AND field_food_restrictions term ID 15
  // AND field_which_menu term ID 24 OR 24
  $result = $entities->execute();

  $nodes = array();
  if (!empty($result['node'])) {
    $nodes = node_load_multiple(array_keys($result['node']));

Looking for support? Visit the forums, or join #drupal-support in IRC.


belvedere’s picture

the final lines should use node_load_multiple() instead of hitting the db for each entity id.