For a given taxonomy term in D8, is there any API function/method that returns all node entities that reference it?

4 Answers 4


You can use loadByProperties() to get all node entities that reference $term_id:

$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties([
  'field_tags' => $term_id,
  • not working for drupal 8
    – Ekta Puri
    Apr 23, 2020 at 2:28
  • works well for drupal 8.9
    – Dimetry
    Oct 9, 2020 at 14:11

Easiest way I think is to query database taxonomy_index table keeps record of what node is tagged with what term

function _get_nodes_by_term($term_id) {
  $query = \Drupal::database()->select('taxonomy_index', 'ti');
  $query->fields('ti', ['nid']);
  $query->condition('ti.tid', $term_id);
  $nodes = $query->execute()->fetchAssoc();
  return $nodes;


Drupal Core does it by views.
Out the box term page display is created by views so if you visit admin/structure/views/view/taxonomy_term, this view is responsible to show all tagged content by given term, which is essentially running database query.

  • Best solution, but I think it should be $nodes = $query->execute()->fetchAll();
    – Baud
    Feb 12, 2021 at 21:49
  • One thing to be aware of with this approach is that it will only get published nodes
    – malcomio
    Aug 19, 2021 at 8:44

If you have multiple term ids you can pass an array of the ids instead of just a single value.

$term_ids =  array (1,2,3);
$nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties([
  'field_tags' => $term_ids,

If your nodes not having revision then this code will work, by default it will return the first revision

  $nodes = \Drupal::entityTypeManager()->getStorage('node')->getQuery()
 ->condition('field_tags', $termId)

If your nodes have multiple revisions, To get the nodes by the latest tagged revision. you need to use the following code

$nodes = \Drupal::entityTypeManager()->getStorage('node')->getQuery()
->condition('field_tags', $termId, '=')

if you have multiple values then you can use array

 $termIds = [3,56,456];
 $nodes = \Drupal::entityTypeManager()->getStorage('node')->getQuery()
 ->condition('field_tags', $termIds)

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.