Drupal 7: taxonomy_select_nodes with multiple terms


/ Published in: PHP
Save to your folder(s)

**Important: this snipplet has moved to Github.**

- [taxonomy_select_nodes with multiple terms, in Drupal 7](https://gist.github.com/1973248)


Copy this code and paste it in your HTML
  1. /**
  2.  * In Drupal 6, `taxonomy_select_nodes` was able to filter by multiple terms,
  3.  * but Drupal 7 accepts only one Term ID.
  4.  */
  5.  
  6. function taxonomy_select_nodes_multiple($tids = array(), $operator = 'or'){
  7. $nids = array();
  8. if (variable_get('taxonomy_maintain_index_table', TRUE)){
  9. switch($operator){
  10. case 'or':
  11. $query = 'SELECT DISTINCT(nid) FROM {taxonomy_index} WHERE tid IN(' . implode(',', $tids) . ')';
  12. break;
  13. case 'and':
  14. $i = 0;
  15. foreach ($tids as $tid){
  16. if ($i == 0){
  17. $tables = 't0.nid FROM {taxonomy_index} t0 ';
  18. $where = "t0.tid = $tid ";
  19. } else {
  20. $tables .= "LEFT JOIN {taxonomy_index} t$i ON (t$i.nid = t".($i - 1).'.nid) ';
  21. $where .= "AND t$i.tid = $tid ";
  22. }
  23. $i ++;
  24. }
  25. $query = "SELECT $tables WHERE $where ORDER BY t0.nid ASC";
  26. break;
  27. }
  28. }
  29. if (!empty($query)) $nids = db_query($query)->fetchCol();
  30. return $nids;
  31. }

URL: http://www.wildpeaks.com

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.