0

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 .

1
  • See \Drupal\Core\Entity\Query\QueryInterface::condition
    – user21641
    Jul 18, 2017 at 7:21

1 Answer 1

0

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.

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.