Query Drupal 7 for nodes sharing both taxonomy terms(tid). I am querying the taxonomy_index tid and nid (and sorting it by created column). The taxonomy_index table has a column for tid and the corresponding nid tagged with that tid.
I have tried multiple combinations but all of them seem to either return all nodes tagged with either tid1 or tid2, or simply just tid1. Some of my code look like:
<!-- Query the taxonomy_index -->
$results = db_select('taxonomy_index', 't')
->fields('t', array('nid', 'tid'))
->distinct()
->condition('t.tid', $tids, 'IN')
->orderBy('t.created', 'DESC')
->extend('PagerDefault')
->limit(9)
->execute()
->fetchCol();
<!-- Build the pager -->
$total = db_select('taxonomy_index', 't')
->fields('t', array('nid', 'tid'))
->distinct()
->condition('t.tid', $tids, 'IN')
->countQuery()
->execute() // Execute the query
->fetchCol(); // Fetch column
$total_pager = $total[0];
pager_default_initialize($total_pager,9);
I have also tried adding:
$query->where('t.tid = 115 AND t.tid = 210'); // Query for specific tids= 115 and 210.
Anybody know a solution to this? Or is there a preferred alternative like building up a new index table or perhaps make a new tid in another vocabulary that contains both (tid1 + tid2 = tid3)?