2

I have a custom vocabulary called "healthcare_terms" that has enabled terms attached to a custom content type called "health_posts".

I just want to find all nodes that are tagged with enabled terms from "healthcare_terms"

I tried the following but it does not seem to be working.

   $query = \Drupal::entityQuery('node')
      ->condition('type', 'health_posts')
      ->condition('status', 1)
      ->condition('field_tags.entity.name', 'healthcare_terms');
    $ids = $query->execute();

Clearly I am lost. Any help or tips?

Thanks guys

3

Are you sure that you are using the tags field for this?

You're filtering on the term name, but you want to filter on the term vid instead:

 $query = \Drupal::entityQuery('node')
  ->condition('type', 'health_posts')
  ->condition('status', 1)
  ->condition('field_tags.entity.vid', 'healthcare_terms');
$ids = $query->execute();

If you have a standalone field that only allows terms from your healthcare_terms vocabulary, which is usually the better approach, then you can just look for ->exists('field_something').

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.