Index: query.php
===================================================================
--- query.php	(revision 11568)
+++ query.php	(working copy)
@@ -1395,6 +1395,25 @@
 				$qv['tag_slug__and'] = array_map('sanitize_title', $qv['tag_slug__and']);
 				$this->is_tag = true;
 			}
+			
+			//Custom Taxonomies
+			$this->custom_taxonomies = array();
+			$this->custom_taxonomies__not_in = array();
+			
+			//loop through registered taxonomies to check for respective query vars
+			foreach ( $GLOBALS['wp_taxonomies'] as $taxonomy => $t ) {
+				if( $taxonomy == 'category' || $taxonomy == 'post_tag' || $taxonomy == 'link_category' ) continue;
+				if( array_key_exists( $taxonomy . '__in', $qv ) ) {
+					$is = 'is_' . $taxonomy;
+					$this->$is = true;
+					$this->custom_taxonomies[$taxonomy . '__in'] = $taxonomy;
+				}
+				elseif( array_key_exists( $taxonomy . '__not_in', $qv ) ) {
+					$is = 'is_' . $taxonomy;
+					$this->$is = true;
+					$this->custom_taxonomies[$taxonomy . '__not_in'] = $taxonomy;
+				}
+			}
 
 			if ( empty($qv['taxonomy']) || empty($qv['term']) ) {
 				$this->is_tax = false;
@@ -1901,28 +1920,44 @@
 					$whichcat .= " AND $wpdb->posts.ID NOT IN ('" . implode("', '", $ids) . "')";
 			}
 		}
+		
+		//Custom Taxonomies
+		foreach( (array) $this->custom_taxonomies as $var => $custom_tax ) {
+		  	$join = " INNER JOIN $wpdb->term_relationships ON ($wpdb->posts.ID = $wpdb->term_relationships.object_id) INNER JOIN $wpdb->term_taxonomy ON ($wpdb->term_relationships.term_taxonomy_id = $wpdb->term_taxonomy.term_taxonomy_id) ";
+		  	$reqtag = is_term( $q[$var][0], $custom_tax );
+		  	$distinct = 'DISTINCT';
+		}
+		
+		//Custom Taxonomy Not In
+		foreach( (array) $this->custom_taxonomies__not_in as $var => $custom_tax ) {
+			if ( $wpdb->has_cap( 'subqueries' ) ) {
+				$tax_string = "'" . implode("', '", $q[$var]) . "'";
+				$whichcat .= " AND $wpdb->posts.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships AS tr INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id WHERE tt.taxonomy = '$custom_tax' AND tt.term_id IN ($tax_string) )";
+			} else {
+				$ids = get_objects_in_term($q[$var], $custom_tax);
+				if ( !is_wp_error($ids) && is_array($ids) && count($ids) > 0 )
+					$whichcat .= " AND $wpdb->posts.ID NOT IN ('" . implode("', '", $ids) . "')";
+			}
+		}
 
-		// Tag and slug intersections.
+		// Tag and slug intersections. Also merge with custom taxonomies
 		$intersections = array('category__and' => 'category', 'tag__and' => 'post_tag', 'tag_slug__and' => 'post_tag', 'tag__in' => 'post_tag', 'tag_slug__in' => 'post_tag');
+		$intersections = array_merge( $intersections, (array) $this->custom_taxonomies );
 		$tagin = array('tag__in', 'tag_slug__in'); // These are used to make some exceptions below
 		foreach ($intersections as $item => $taxonomy) {
 			if ( empty($q[$item]) ) continue;
-			if ( in_array($item, $tagin) && empty($q['cat']) ) continue; // We should already have what we need if categories aren't being used
-
+						
 			if ( $item != 'category__and' ) {
 				$reqtag = is_term( $q[$item][0], 'post_tag' );
 				if ( !empty($reqtag) )
 					$q['tag_id'] = $reqtag['term_id'];
 			}
 
-			if ( in_array( $item, array('tag_slug__and', 'tag_slug__in' ) ) )
-				$taxonomy_field = 'slug';
-			else
-				$taxonomy_field = 'term_id';
+			$taxonomy_field = $item != ('tag_slug__and' || 'tag_slug__in') ? 'slug' : 'term_id';
 
 			$q[$item] = array_unique($q[$item]);
 			$tsql = "SELECT p.ID FROM $wpdb->posts p INNER JOIN $wpdb->term_relationships tr ON (p.ID = tr.object_id) INNER JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) INNER JOIN $wpdb->terms t ON (tt.term_id = t.term_id)";
-			$tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";
+			$tsql .= " WHERE tt.taxonomy = '$taxonomy' AND t.$taxonomy_field IN ('" . implode("', '", $q[$item]) . "')";			
 			if ( !in_array($item, $tagin) ) { // This next line is only helpful if we are doing an and relationship
 				$tsql .= " GROUP BY p.ID HAVING count(p.ID) = " . count($q[$item]);
 			}
@@ -2697,4 +2732,4 @@
 	return true;
 }
 
-?>
+?>
\ No newline at end of file

