Index: query.php
===================================================================
--- query.php	(revision 19268)
+++ query.php	(working copy)
@@ -1949,9 +1949,11 @@
 		$join = '';
 		$search = '';
 		$groupby = '';
-		$fields = '';
+		$fields = "$wpdb->posts.ID";
 		$post_status_join = false;
 		$page = 1;
+                $orderby = '';
+                $quick_fields = "$wpdb->posts.*";
 
 		if ( isset( $q['caller_get_posts'] ) ) {
 			_deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) );
@@ -2033,12 +2035,15 @@
 		switch ( $q['fields'] ) {
 			case 'ids':
 				$fields = "$wpdb->posts.ID";
+                                $quick_fields = "$wpdb->posts.ID";
 				break;
 			case 'id=>parent':
 				$fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
+                                $quick_fields = "$wpdb->posts.ID, $wpdb->posts.post_parent";
 				break;
 			default:
-				$fields = "$wpdb->posts.*";
+				$fields = "$wpdb->posts.ID";
+				$quick_fields = "$wpdb->posts.*";
 		}
 
 		// If a month is specified in the querystring, load that month
@@ -2564,21 +2569,44 @@
 
 		$pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
 
+                // Set up quick_* defaults from the standard placeholders, before filtering
+                $quick_pieces = array( 'quick_where', 'quick_groupby', 'quick_join', 'quick_orderby', 'quick_distinct', 'quick_fields', 'quick_limits' );
+                $quick_distinct = $distinct;
+		$quick_join = $join;
+		$quick_where = $where;
+		$quick_groupby = $groupby;
+                
 		// 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 ) );
+                        $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this, false ) );
+			$quick_distinct = apply_filters_ref_array( 'posts_distinct', array( $quick_distinct, &$this, true ) );
+
+			$fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this, false ) );
+			$quick_fields = apply_filters_ref_array( 'posts_fields', array( $quick_fields, &$this, true ) );
+
+			$join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this, false ) );
+			$quick_join = apply_filters_ref_array( 'posts_join_paged', array( $quick_join, &$this, true ) );
+
+			$where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this, false ) );
+			$quick_where = apply_filters_ref_array( 'posts_where_paged', array( $quick_where, &$this, true ) );
+
+			$groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this, false ) );
+			$quick_groupby = apply_filters_ref_array( 'posts_groupby', array( $quick_groupby, &$this, true ) );
+
 			$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 ) );
-
+			
 			// Filter all clauses at once, for convenience
 			$clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
 			foreach ( $pieces as $piece )
 				$$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
+
+                        // And again for the quick clauses
+                        $clauses = (array) apply_filters_ref_array( 'posts_quick_clauses', array( compact( $quick_pieces ), &$this ) );
+                        foreach ( $quick_pieces as $piece )
+                            $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 
 		}
 
 		// Announce current selection parameters.  For use by caching plugins.
@@ -2586,22 +2614,40 @@
 
 		// 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 ) );
+			
+                        $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this, false ) );
+			$quick_distinct = apply_filters_ref_array( 'posts_distinct_request', array( $quick_distinct, &$this, true ) );
+
+			$fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this, false ) );
+			$quick_fields = apply_filters_ref_array( 'posts_fields_request', array( $quick_fields, &$this, true ) );
+
+			$join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this, false ) );
+			$quick_join = apply_filters_ref_array( 'posts_join_request', array( $quick_join, &$this, true ) );
+
+			$where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this, false ) );
+			$quick_where = apply_filters_ref_array( 'posts_where_request', array( $quick_where, &$this, true ) );
+
+			$groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this, false ) );
+			$quick_groupby = apply_filters_ref_array( 'posts_groupby_request', array( $quick_groupby, &$this, true ) );
+
 			$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 ) );
 
 			// Filter all clauses at once, for convenience
 			$clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
 			foreach ( $pieces as $piece )
 				$$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
+
+                        // And again for the quick clauses
+                        $clauses = (array) apply_filters_ref_array( 'posts_quick_clauses_request', array( compact( $quick_pieces ), &$this ) );
+                        foreach ( $quick_pieces as $piece )
+                            $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : ''; 
 		}
 
 		if ( ! empty($groupby) )
 			$groupby = 'GROUP BY ' . $groupby;
+                if ( ! empty($quick_groupby) )
+			$quick_groupby = 'GROUP BY ' . $quick_groupby;
 		if ( !empty( $orderby ) )
 			$orderby = 'ORDER BY ' . $orderby;
 
@@ -2609,28 +2655,69 @@
 		if ( !$q['no_found_rows'] && !empty($limits) )
 			$found_rows = 'SQL_CALC_FOUND_ROWS';
 
-		$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 ) );
+		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 $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby";
 
-		if ( 'ids' == $q['fields'] ) {
-			$this->posts = $wpdb->get_col($this->request);
+                        if ( 'ids' == $q['fields'] ) {
+                            $this->posts = $wpdb->get_col($this->request);
+                            return $this->posts;
+                        }
 
-			return $this->posts;
-		}
+                        if ( 'id=>parent' == $q['fields'] ) {
+                            $this->posts = $wpdb->get_results($this->request);
 
-		if ( 'id=>parent' == $q['fields'] ) {
-			$this->posts = $wpdb->get_results($this->request);
-
-			$r = array();
-			foreach ( $this->posts as $post )
+                            $r = array();
+                            foreach ( $this->posts as $post )
 				$r[ $post->ID ] = $post->post_parent;
 
-			return $r;
+                            return $r;
+                        }
+
+			if ( !$q['suppress_filters'] ) {
+				$this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this, false ) );
+				$this->quick_request = apply_filters_ref_array('posts_request', array( $this->quick_request, &$this, true ) );
+			}
+
+			$this->posts = $wpdb->get_results($this->quick_request);
+
+			$this->found_posts = count($this->posts);
+			$this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
+			$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, false ) );
+
+			$post_ids = $wpdb->get_col($this->request);
+
+			if ( !$post_ids ) {
+				$this->found_posts = 0;
+				$this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
+				$this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
+
+				$this->quick_request = " SELECT $quick_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_ref_array('posts_request', array( $this->quick_request, &$this, true ) );
+
+				$this->posts = array(); // no point in querying, since there are no posts
+			} else {
+				$found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
+				$this->found_posts = $wpdb->get_var( $found_posts_query );
+				$this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
+				$this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
+
+				$this->quick_request = " SELECT $quick_distinct $quick_fields 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_ref_array('posts_request', array( $this->quick_request, &$this, true ) );
+
+				$this->posts = $wpdb->get_results($this->quick_request);
+			}
 		}
 
-		$this->posts = $wpdb->get_results($this->request);
-
 		// Raw results filter.  Prior to status checks.
 		if ( !$q['suppress_filters'] )
 			$this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
@@ -2647,14 +2734,7 @@
 			$this->comments = $wpdb->get_results($comments_request);
 			$this->comment_count = count($this->comments);
 		}
-
-		if ( !$q['no_found_rows'] && !empty($limits) ) {
-			$found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
-			$this->found_posts = $wpdb->get_var( $found_posts_query );
-			$this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
-			$this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
-		}
-
+                
 		// Check post status to determine if post should be displayed.
 		if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
 			$status = get_post_status($this->posts[0]);
