Revision: 43903
Updated Code
at March 5, 2012 02:42 by wildpeaks
Updated Code
/**
* In Drupal 6, `taxonomy_select_nodes` was able to filter by multiple terms,
* but Drupal 7 accepts only one Term ID.
*/
function taxonomy_select_nodes_multiple($tids = array(), $operator = 'or'){
$nids = array();
if (variable_get('taxonomy_maintain_index_table', TRUE)){
switch($operator){
case 'or':
$query = 'SELECT DISTINCT(nid) FROM {taxonomy_index} WHERE tid IN(' . implode(',', $tids) . ')';
break;
case 'and':
$i = 0;
foreach ($tids as $tid){
if ($i == 0){
$tables = 't0.nid FROM {taxonomy_index} t0 ';
$where = "t0.tid = $tid ";
} else {
$tables .= "LEFT JOIN {taxonomy_index} t$i ON (t$i.nid = t".($i - 1).'.nid) ';
$where .= "AND t$i.tid = $tid ";
}
$i ++;
}
$query = "SELECT $tables WHERE $where ORDER BY t0.nid ASC";
break;
}
}
if (!empty($query)) $nids = db_query($query)->fetchCol();
return $nids;
}
Revision: 43902
Updated Code
at April 2, 2011 11:28 by wildpeaks
Updated Code
function taxonomy_select_nodes_multiple($tids = array(), $operator = 'or'){
$nids = array();
if (variable_get('taxonomy_maintain_index_table', TRUE)){
switch($operator){
case 'or':
$query = 'SELECT DISTINCT(nid) FROM {taxonomy_index} WHERE tid IN(' . implode(',', $tids) . ')';
break;
case 'and':
$i = 0;
foreach ($tids as $tid){
if ($i == 0){
$tables = 't0.nid FROM {taxonomy_index} t0 ';
$where = "t0.tid = $tid ";
} else {
$tables .= "LEFT JOIN {taxonomy_index} t$i ON (t$i.nid = t".($i - 1).'.nid) ';
$where .= "AND t$i.tid = $tid ";
}
$i ++;
}
$query = "SELECT $tables WHERE $where ORDER BY t0.nid ASC";
break;
}
}
if (!empty($query)) $nids = db_query($query)->fetchCol();
return $nids;
}
Revision: 43901
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at April 2, 2011 11:26 by wildpeaks
Initial Code
function taxonomy_select_nodes_multiple($tids = array(), $operator = 'or'){
$nids = array();
if (variable_get('taxonomy_maintain_index_table', TRUE)){
switch($operator){
case 'or':
$query = 'SELECT DISTINCT(nid) FROM taxonomy_index WHERE tid IN(' . implode(',', $tids) . ')';
break;
case 'and':
$i = 0;
foreach ($tids as $tid){
if ($i == 0){
$tables = 't0.nid FROM {taxonomy_index} t0 ';
$where = "t0.tid = $tid ";
} else {
$tables .= "LEFT JOIN {taxonomy_index} t$i ON (t$i.nid = t".($i - 1).'.nid) ';
$where .= "AND t$i.tid = $tid ";
}
$i ++;
}
$query = "SELECT $tables WHERE $where ORDER BY t0.nid ASC";
break;
}
}
if (!empty($query)) $nids = db_query($query)->fetchCol();
return $nids;
}
Initial URL
http://www.wildpeaks.com
Initial Description
**Important: this snipplet has moved to Github.** - [taxonomy_select_nodes with multiple terms, in Drupal 7](https://gist.github.com/1973248)
Initial Title
Drupal 7: taxonomy_select_nodes with multiple terms
Initial Tags
Initial Language
PHP