Drupal 7: Get all content items attached to all terms in a vocabulary sorted by date

1k views Asked by At

Just like the title says. I have a vocabulary with several terms. I'd like to be able to get all items tagged by each of the terms in the vocabulary sorted by date created in one giant list. Is this possible with the built-in taxonomy API or am I going to have to build something custom?

2

There are 2 answers

2
Anthony Leach On

When you say 'items' if you mean nodes then the function you are looking for is taxonomy_select_nodes(). https://api.drupal.org/api/drupal/modules%21taxonomy%21taxonomy.module/function/taxonomy_select_nodes/7

This function returns an array of node ids that match have the term selected.

/**
 * Get an array of node id's that match terms in a given vocab.
 * 
 * @param string $vocab_machine_name
 *   The vocabulary machine name.
 * 
 * @return array
 *   An array of node ids.
 */
function _example_get_vocab_nids($vocab_machine_name) {
  $vocabulary = taxonomy_vocabulary_machine_name_load($vocab_machine_name);
  if (!$vocabulary) {
    return array();
  }

  $terms = taxonomy_get_tree($vocabulary->vid);
  if (!$terms) {
    return array();
  }

  $nids = array();
  foreach ($terms as $term) {
    $term_nids = taxonomy_select_nodes($term->tid, FALSE);
    $nids = array_merge($nids, $term_nids);
  }

  return $nids;  
}
0
Muhammad Reda On

As far as I know, there is no function/method in Drupal API will get that for you. I believe you will have to make your own.