Posted By

wildpeaks on 04/02/11


Tagged

drupal7 taxonomy


Versions (?)

Drupal 7: taxonomy_select_nodes with multiple terms


 / Published in: PHP
 

URL: http://www.wildpeaks.com

Important: this snipplet has moved to Github.

  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. }

Report this snippet  

You need to login to post a comment.