Skip to content

Instantly share code, notes, and snippets.

@midnai
Created February 8, 2018 06:29
Show Gist options
  • Save midnai/d828328995205a657bb59dbfef52ff7f to your computer and use it in GitHub Desktop.
Save midnai/d828328995205a657bb59dbfef52ff7f to your computer and use it in GitHub Desktop.
get nodes of taxonomy term(s) in drupal 8
function getNodesByTaxonomyTermIds($termIds){
$termIds = (array) $termIds;
if(empty($termIds)){
return NULL;
}
$query = \Drupal::database()->select('taxonomy_index', 'ti');
$query->fields('ti', array('nid'));
$query->condition('ti.tid', $termIds, 'IN');
$query->distinct(TRUE);
$result = $query->execute();
if($nodeIds = $result->fetchCol()){
return Node::loadMultiple($nodeIds);
}
return NULL;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment