Perform a query with an entity field condition with multiple values I have a content type that has a entity reference field that allows users to add multiple taxonomy terms in that field. I'm attempting to perform queries that grab nodes that have a specific set of taxonomy terms within that field.

Using one value in that field works fine, like so.

    $query = \Drupal::entityQuery('node')
        -&gt;condition('status', NODE_PUBLISHED)
        -&gt;condition('type', 'custom_type')
        -&gt;condition('custom_taxonomy', 2)
        -&gt;sort('field_last_name', DESC);


Where 2 is the id of the term I'm searching for. However when I attempt to search for nodes that contain two specific terms like so,

    $query = \Drupal::entityQuery('node')
        -&gt;condition('status', NODE_PUBLISHED)
        -&gt;condition('type', 'custom_type')
        -&gt;condition('custom_taxonomy', [2,8])
        -&gt;sort('field_last_name', DESC);


I receive the error


  Invalid parameter number: number of bound variables does not match
  number of tokens:


I've also attempted

    $query = \Drupal::entityQuery('node')
        -&gt;condition('status', NODE_PUBLISHED)
        -&gt;condition('type', 'custom_type')
        -&gt;condition('custom_taxonomy', [2,8], 'IN')
        -&gt;sort('field_last_name', DESC);


Which doesn't fail, but doesn't provide the intended results. It displays every node that has either term 2 OR term 8. Instead of term 2 AND term 8 as intended.
How would I perform a query that checks if a node has multiple specific values in an entity reference field?
Use two separate andConditionGroup():

$query = \Drupal::entityQuery('node')
  -&gt;condition('status', NODE_PUBLISHED)
  -&gt;condition('type', 'custom_type');
$and = $query-&gt;andConditionGroup();
$and-&gt;condition('custom_taxonomy', 2);
$query-&gt;condition($and);
$and = $query-&gt;andConditionGroup();
$and-&gt;condition('custom_taxonomy', 8);
$query-&gt;condition($and);
$result = $query-&gt;execute();


This works no matter how many terms are in the field or in which delta they are.

Edit

This results in this SQL:

SELECT base_table.vid AS vid, base_table.nid AS nid
FROM 
{node} base_table
INNER JOIN {node_field_data} node_field_data ON node_field_data.nid = base_table.nid
INNER JOIN {node__custom_taxonomy} node__custom_taxonomy ON node__custom_taxonomy.entity_id = base_table.nid
INNER JOIN {node__custom_taxonomy} node__custom_taxonomy_2 ON node__custom_taxonomy_2.entity_id = base_table.nid
WHERE  (node_field_data.status = '1') AND (node_field_data.type = 'custom_type') AND( (node__custom_taxonomy.custom_taxonomy_target_id = '2') )AND( (node__custom_taxonomy_2.custom_taxonomy_target_id = '8') )