I am getting an empty array when I query by the value of a referenced field.

I have the following:

$result = \Drupal::entityQuery('node')
  ->condition('type', 'custom')
  ->condition('status', 1)
  ->condition('field_category.entity:taxonomy_term.name', 'somevalue', '=')
  // ->condition('field_category.%delta', 'somevalue');

Using ->condition('field_category.%delta', 'somevalue'); returns way too many results (nids) than expected.

using ->condition('field_category.entity:taxonomy_term', 'somevalue', '='): returns an empty array.

Am I using an incorrect syntax to query by a related field?

1 Answer 1


As mentioned here it seems like you only need .entity like this:

->condition('field_category.entity.name', 'somevalue', '=');
  • Thanks Jdrupal - I tried that too, and it didnt work. I might need to look into the structure a bit more as the query looks ok to me.
    – Jan K
    Jul 26, 2019 at 10:33
  • Did you ever get this to work?
    – user72672
    Aug 20, 2019 at 7:50

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.