I have a content type with two taxonomy fields using the same vocabulary, I need a view with two exposed filters that, when both set to the same term, show just the nodes with that term in both taxonomy fields.

The current result is that when I use two "Content: Has taxonomy term (exposed)" filters and I select the first one to "term 1", I get all the nodes with "term 1" in the two taxonomy fields. When I select in the second filter to "term 1" onother time, I get the same set of results as before. What I would like to achieve is the following: when selecting "Term 1" in both the exposed filters I'd like to have just the nodes that have "term 1" in both the taxonomy fields.

How can I get this result?


  • It sounds like you want to use an exposed filter for each field rather than two "Content: Has taxonomy term" – thumbson Jan 18 '18 at 19:18
  • Thank you but that's not the intended behaviour. When using the first filter it should show all the nodes with the selected term in one of the two taxonomy fields. Just like using the "Content: Has taxonomy term" filter. The only issue is that when selecting two times the same term both filters it should show nodes that have two times the same term, one in each field. That's the required logic that I've not been able to achieve. – Oto Tortorella Jan 18 '18 at 21:03

One way to do this would be to use a hook_query_alter() in a custom module. In your view you would set a tag in the query options ("same_terms" in this example).

Then check if your exposed filter values are the same (you may need to change tid and tid_1 depending on your identifiers).

Finally you'd join the field tables which by itself would limit the result set to only nodes that have the same term in both fields. You would need to swap field_tags and field_tag_2 with your field names.

use Drupal\Core\Database\Query\AlterableInterface;

 * Implements hook_query_alter().
function YOURMODULE_query_alter(AlterableInterface $query) {
  // make sure you're only altering the right query.
  if ($query->hasTag('same_terms')) {
    $view = views_get_current_view();
    // If the exposed filter values are the same.
    if ($view->exposed_data['tid'] === $view->exposed_data['tid_1']) {
      $query->join('node__field_tags', 'field_1', 'field_1.entity_id = node_field_data.nid AND field_1.field_tags_target_id = taxonomy_index_value_0.tid');
      $query->join('node__field_tag_2', 'field_2', 'field_2.entity_id = node_field_data.nid AND field_2.field_tag_2_target_id = taxonomy_index_value_0.tid');

Alternatively if the filters don't need to be interchangeable, you could make the first one Content: Has taxonomy term and the second one filter the second taxonomy field specifically, which should almost accomplish the same thing. The downside here is that if only the second filter had a value it wouldn't filter on any term, only the second term.

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.