For the filter block I need to get the data from database. I have three tables "node", "node__filed_country" and "taxonomy_index". So I need to pull only those taxonomies which are realted to the nodes, which field_country is equal to three particular countries. So I need to provide two joins.

$query = $this->connection->select('node', 'node');
$query->join('node__field_country', $nfc, "node.nid=$nfc.entity_id and ($nfc.field_country_target_id=1 or $nfc.field_country_target_id=2 or $nfc.field_country_target_id=3)");
$query->join('taxonomy_index', $id, "$id.nid = node.nid AND $id.tid = $term->tid AND $id.status = 1 ");

But something doesnt work in this code. Could you please help me?

2 Answers 2


You try code

$term_id = 1;
  $query = $this->connection->select('node', 'node');
  $query->join('node__field_country', "nfc", "node.nid=nfc.entity_id and (nfc.field_country_target_id=1 or nfc.field_country_target_id=2 or nfc.field_country_target_id=3)");
  $query->join('taxonomy_index', "tid", "tid.nid = node.nid AND tid.tid =".$term_id." AND tid.status = 1 ");
  $query = $this->connection->select('node', 'node');
  $query->join('node__field_country', $nfc, "node.nid=$nfc.entity_id");
  $query->join('taxonomy_index', $id, "$id.nid = node.nid");
  $or_condition = $query->orConditionGroup()
    ->condition("$nfc.field_country_target_id", '1')
    ->condition("$nfc.field_country_target_id", '2')
    ->condition("$nfc.field_country_target_id", '3');
  $and_condition = $query->andConditionGroup()
    ->condition("$id.tid", "$term->tid")
    ->condition("$id.status", '1');

You have to use or and and conditions separately and append it to query that you are building.

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.