Hello I have created content type product. I also add Taxonomy with name "category". I add field in with term reference in product content type. Now I want to get all content with taxonomy name and id which have content type named "product" using query in Drupal 8 .

  • See \Drupal\Core\Entity\Query\QueryInterface::condition – user21641 Jul 18 '17 at 7:21

Make a dynamic query like this:

$query = \Drupal::database()->select('node_field_data', 'nfd');
$query->condition('nfd.type', 'product');
$query->leftJoin('node__field_category', 'ft', 'ft.entity_id = nfd.nid');
$query->leftJoin('taxonomy_term_field_data', 'fd', 'fd.tid = ft.field_category_target_id');
$query->fields('fd', ['tid', 'name']);
$query->fields('nfd', ['nid']);
$results = $query->execute()->fetchAll();

Suppose the reference field taxonomy machine name is "node__field_category". The result of the query will be node id , term name and term id.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.