Index: query.php
===================================================================
--- query.php	(revision 11993)
+++ query.php	(working copy)
@@ -1396,6 +1396,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;
@@ -1596,7 +1615,7 @@
 			if ( $this->is_search )
 				$q['post_type'] = 'any';
 			else
-				$q['post_type'] = '';
+				$q['post_type'] = 'post';
 		}
 		$post_type = $q['post_type'];
 		if ( !isset($q['posts_per_page']) || $q['posts_per_page'] == 0 )
@@ -1756,7 +1775,7 @@
 				$search .= "{$searchand}(($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}'))";
 				$searchand = ' AND ';
 			}
-			$term = esc_sql($q['s']);
+			$term = $wpdb->escape($q['s']);
 			if (empty($q['sentence']) && count($q['search_terms']) > 1 && $q['search_terms'][0] != $q['s'] )
 				$search .= " OR ($wpdb->posts.post_title LIKE '{$n}{$term}{$n}') OR ($wpdb->posts.post_content LIKE '{$n}{$term}{$n}')";
 
@@ -1902,28 +1921,43 @@
 					$whichcat .= " AND $wpdb->posts.ID NOT IN ('" . implode("', '", $ids) . "')";
 			}
 		}
+		
+		//Custom Taxonomies
+		foreach( (array) $this->custom_taxonomies as $var => $custom_tax ) {
+		  	$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]);
 			}
@@ -2070,21 +2104,16 @@
 				$q['orderby'] = "$wpdb->posts.post_date ".$q['order'];
 		}
 
-		$post_type_cap = $post_type;
-
 		if ( 'any' == $post_type ) {
 			$where .= " AND $wpdb->posts.post_type != 'revision'";
-		} elseif ( ! empty( $post_type ) ) {
-			$where .= " AND $wpdb->posts.post_type = '$post_type'";
 		} elseif ( $this->is_attachment ) {
 			$where .= " AND $wpdb->posts.post_type = 'attachment'";
-			$post_type_cap = 'post';
 		} elseif ($this->is_page) {
 			$where .= " AND $wpdb->posts.post_type = 'page'";
-			$post_type_cap = 'page';
+		} elseif ($this->is_single) {
+			$where .= " AND $wpdb->posts.post_type = 'post'";
 		} else {
-			$where .= " AND $wpdb->posts.post_type = 'post'";
-			$post_type_cap = 'post';
+			$where .= " AND $wpdb->posts.post_type = '$post_type'";
 		}
 
 		if ( isset($q['post_status']) && '' != $q['post_status'] ) {
@@ -2104,8 +2133,6 @@
 				$p_status[] = "$wpdb->posts.post_status = 'private'";
 			if ( in_array( 'publish', $q_status ) )
 				$r_status[] = "$wpdb->posts.post_status = 'publish'";
-			if ( in_array( 'trash', $q_status ) )
-				$r_status[] = "$wpdb->posts.post_status = 'trash'";
 
 			if ( empty($q['perm'] ) || 'readable' != $q['perm'] ) {
 				$r_status = array_merge($r_status, $p_status);
@@ -2113,13 +2140,13 @@
 			}
 
 			if ( !empty($r_status) ) {
-				if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can("edit_others_{$post_type_cap}s") )
+				if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can("edit_others_{$post_type}s") )
 					$statuswheres[] = "($wpdb->posts.post_author = $user_ID " .  "AND (" . join( ' OR ', $r_status ) . "))";
 				else
 					$statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
 			}
 			if ( !empty($p_status) ) {
-				if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can("read_private_{$post_type_cap}s") )
+				if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can("read_private_{$post_type}s") )
 					$statuswheres[] = "($wpdb->posts.post_author = $user_ID " .  "AND (" . join( ' OR ', $p_status ) . "))";
 				else
 					$statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
@@ -2138,7 +2165,7 @@
 				$where .= " OR $wpdb->posts.post_status = 'future' OR $wpdb->posts.post_status = 'draft' OR $wpdb->posts.post_status = 'pending'";
 
 			if ( is_user_logged_in() ) {
-				$where .= current_user_can( "read_private_{$post_type_cap}s" ) ? " OR $wpdb->posts.post_status = 'private'" : " OR $wpdb->posts.post_author = $user_ID AND $wpdb->posts.post_status = 'private'";
+				$where .= current_user_can( "read_private_{$post_type}s" ) ? " OR $wpdb->posts.post_status = 'private'" : " OR $wpdb->posts.post_author = $user_ID AND $wpdb->posts.post_status = 'private'";
 			}
 
 			$where .= ')';
@@ -2296,7 +2323,7 @@
 				} else {
 					if  (in_array($status, array('draft', 'pending')) ) {
 						// User must have edit permissions on the draft to preview.
-						if (! current_user_can("edit_$post_type_cap", $this->posts[0]->ID)) {
+						if (! current_user_can('edit_post', $this->posts[0]->ID)) {
 							$this->posts = array();
 						} else {
 							$this->is_preview = true;
@@ -2304,17 +2331,17 @@
 						}
 					}  else if ('future' == $status) {
 						$this->is_preview = true;
-						if (!current_user_can("edit_$post_type_cap", $this->posts[0]->ID)) {
+						if (!current_user_can('edit_post', $this->posts[0]->ID)) {
 							$this->posts = array ( );
 						}
 					} else {
-						if (! current_user_can("read_$post_type_cap", $this->posts[0]->ID))
+						if (! current_user_can('read_post', $this->posts[0]->ID))
 							$this->posts = array();
 					}
 				}
 			}
 
-			if ( $this->is_preview && current_user_can( "edit_{$post_type_cap}", $this->posts[0]->ID ) )
+			if ( $this->is_preview && current_user_can( "edit_{$post_type}", $this->posts[0]->ID ) )
 				$this->posts[0] = apply_filters('the_preview', $this->posts[0]);
 		}
 
@@ -2701,8 +2728,8 @@
 	}
 
 	do_action_ref_array('the_post', array(&$post));
-
+	
 	return true;
 }
 
-?>
+?>
\ No newline at end of file

