Index: query.php
===================================================================
--- query.php	(revision 13915)
+++ query.php	(working copy)
@@ -1608,15 +1608,21 @@
 
 		// First let's clear some variables
 		$distinct = '';
+		$quick_distinct = '';
+		$fields = "$wpdb->posts.ID"; // see #10964
+		$quick_fields = "$wpdb->posts.*";
 		$whichcat = '';
 		$whichauthor = '';
 		$whichmimetype = '';
+		$search = '';
+		$join = '';
+		$quick_join = '';
 		$where = '';
+		$quick_where = '';
+		$groupby = '';
+		$quick_groupby = '';
+		$orderby = '';
 		$limits = '';
-		$join = '';
-		$search = '';
-		$groupby = '';
-		$fields = "$wpdb->posts.*";
 		$post_status_join = false;
 		$page = 1;
 
@@ -2357,13 +2363,23 @@
 		// Apply post-paging filters on where and join.  Only plugins that
 		// manipulate paging queries should use these hooks.
 		if ( !$q['suppress_filters'] ) {
-			$where		= apply_filters_ref_array( 'posts_where_paged',	array( $where, &$this ) );
-			$groupby	= apply_filters_ref_array( 'posts_groupby',		array( $groupby, &$this ) );
-			$join		= apply_filters_ref_array( 'posts_join_paged',	array( $join, &$this ) );
-			$orderby	= apply_filters_ref_array( 'posts_orderby',		array( $orderby, &$this ) );
-			$distinct	= apply_filters_ref_array( 'posts_distinct',	array( $distinct, &$this ) );
-			$limits		= apply_filters_ref_array( 'post_limits',		array( $limits, &$this ) );
-			$fields		= apply_filters_ref_array( 'posts_fields',		array( $fields, &$this ) );
+			$distinct = apply_filters_ref_array('posts_distinct', array( $distinct, &$this ) );
+			$quick_distinct = apply_filters_ref_array('posts_distinct', array( $quick_distinct, &$this ) );
+
+			$fields = apply_filters_ref_array('posts_fields', array( $fields, &$this ) );
+			$quick_fields = apply_filters_ref_array('posts_fields', array( $quick_fields, &$this ) );
+
+			$join = apply_filters_ref_array('posts_join_paged', array( $join, &$this ) );
+			$quick_join = apply_filters_ref_array('posts_join_paged', array( $quick_join, &$this) );
+
+			$where = apply_filters_ref_array('posts_where_paged', array( $where, &$this ) );
+			$quick_where = apply_filtersref_array('posts_where_paged', array( $quick_where, &$this ) );
+
+			$groupby = apply_filters_ref_array('posts_groupby', array( $groupby, &$this ) );
+			$quick_groupby = apply_filters_ref_array('posts_groupby', $quick_groupby,  &$this ) );
+
+			$orderby = apply_filters_ref_array('posts_orderby', array( $orderby, &$this ) );
+			$limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this )  );
 		}
 
 		// Announce current selection parameters.  For use by caching plugins.
@@ -2371,13 +2387,23 @@
 
 		// Filter again for the benefit of caching plugins.  Regular plugins should use the hooks above.
 		if ( !$q['suppress_filters'] ) {
-			$where		= apply_filters_ref_array( 'posts_where_request',	array( $where, &$this ) );
-			$groupby	= apply_filters_ref_array( 'posts_groupby_request',		array( $groupby, &$this ) );
-			$join		= apply_filters_ref_array( 'posts_join_request',	array( $join, &$this ) );
-			$orderby	= apply_filters_ref_array( 'posts_orderby_request',		array( $orderby, &$this ) );
-			$distinct	= apply_filters_ref_array( 'posts_distinct_request',	array( $distinct, &$this ) );
-			$fields		= apply_filters_ref_array( 'posts_fields_request',		array( $fields, &$this ) );
-			$limits		= apply_filters_ref_array( 'post_limits_request',		array( $limits, &$this ) );
+			$distinct = apply_filters_ref_array('posts_distinct_request', array( $distinct, &$this ) );
+			$quick_distinct = apply_filters_ref_array('posts_distinct_request', $array( quick_distinct, &$this ) );
+
+			$fields = apply_filters_ref_array('posts_fields_request', array( $fields, &$this ) );
+			$quick_fields = apply_filters_ref_array('posts_fields_request', array( $quick_fields, &$this ) );
+
+			$join = apply_filters_ref_array('posts_join_request', array( $join, &$this ) );
+			$quick_join = apply_filters_ref_array('posts_join_request', array( $quick_join, &$this ) );
+
+			$where = apply_filters_ref_array('posts_where_request', array( $where, &$this ) );
+			$quick_where = apply_filters_ref_array('posts_where_request', array( $quick_where, &$this ) );
+
+			$groupby = apply_filters_ref_array('posts_groupby_request', array( $groupby, &$this ) );
+			$quick_groupby = apply_filters_ref_array('posts_groupby_request', array( $quick_groupby, &$this ) );
+
+			$orderby = apply_filters_ref_array('posts_orderby_request', array( $orderby, &$this ) );
+			$limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
 		}
 
 		if ( ! empty($groupby) )
@@ -2388,11 +2414,51 @@
 		if ( !$q['no_found_rows'] && !empty($limits) )
 			$found_rows = 'SQL_CALC_FOUND_ROWS';
 
+		if ( empty($limits) ) {
+			// do a direct query, as there is no benefit in fetching a huge load of IDs in an IN clause
+			$this->request = " SELECT $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby";
+			$this->quick_request = " SELECT $distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $where $quick_groupby $orderby";
+
+			if ( !$q['suppress_filters'] ) {
+				$this->request = apply_filters('posts_request', $this->request, false);
+				$this->quick_request = apply_filters('posts_request', $this->quick_request, true);
+			}
+
+			$this->posts = $wpdb->get_results($this->quick_request);
+
+			$this->found_posts = count($this->posts);
+			$this->found_posts = apply_filters( 'found_posts', $this->found_posts );
+			$this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
+		} else {
 		$this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
 		if ( !$q['suppress_filters'] )
 			$this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) );
+	             
+	               $post_ids = $wpdb->get_col($this->request);
 
-		$this->posts = $wpdb->get_results($this->request);
+			if ( !$post_ids ) {
+				$this->found_posts = 0;
+				$this->found_posts = apply_filters( 'found_posts', $this->found_posts );
+				$this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
+
+				$this->quick_request = " SELECT $distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby";
+				if ( !$q['suppress_filters'] )
+					$this->quick_request = apply_filters('posts_request', $this->quick_request, true);
+				$this->posts = array(); // no point in querying, since there are no posts
+			} else {
+				$found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
+				$this->found_posts = $wpdb->get_var( $found_posts_query );
+				$this->found_posts = apply_filters( 'found_posts', $this->found_posts );
+				$this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
+
+				$this->quick_request = " SELECT $quick_distinct * FROM $wpdb->posts $quick_join WHERE 1 = 1 $quick_where AND $wpdb->posts.ID IN ( " . implode(',', $post_ids) . " ) $quick_groupby ORDER BY FIELD( $wpdb->posts.ID, " . implode(',', $post_ids) . ") ";
+				
+				if ( !$q['suppress_filters'] )
+					$this->quick_request = apply_filters('posts_request', $this->quick_request, true);
+
+				$this->posts = $wpdb->get_results($this->quick_request);
+			}
+		}
 		// Raw results filter.  Prior to status checks.
 		if ( !$q['suppress_filters'] )
 			$this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
