WordPress 3.4
[autoinstalls/wordpress.git] / wp-includes / query.php
index 0fcbf94cb51e6b5819d86816581a1da59d5f873e..5f6ccddfb8a10a1b0e302f5ac74b561f03cff199 100644 (file)
@@ -27,9 +27,8 @@ function get_query_var($var) {
        return $wp_query->get($var);
 }
 
-
 /**
- * Retrieve the currently-queried object.  Wrapper for $wp_query->get_queried_object()
+ * Retrieve the currently-queried object. Wrapper for $wp_query->get_queried_object()
  *
  * @uses WP_Query::get_queried_object
  *
@@ -728,7 +727,7 @@ function is_main_query() {
 }
 
 /*
- * The Loop.  Post loop control.
+ * The Loop. Post loop control.
  */
 
 /**
@@ -1261,7 +1260,7 @@ class WP_Query {
        var $query_vars_hash = false;
 
        /**
-        * Whether query vars have changed since the initial parse_query() call.  Used to catch modifications to query vars made
+        * Whether query vars have changed since the initial parse_query() call. Used to catch modifications to query vars made
         * via pre_get_posts hooks.
         *
         * @since 3.1.1
@@ -1454,7 +1453,7 @@ class WP_Query {
                if ( '' !== $qv['minute'] ) $qv['minute'] = absint($qv['minute']);
                if ( '' !== $qv['second'] ) $qv['second'] = absint($qv['second']);
 
-               // Compat.  Map subpost to attachment.
+               // Compat. Map subpost to attachment.
                if ( '' != $qv['subpost'] )
                        $qv['attachment'] = $qv['subpost'];
                if ( '' != $qv['subpost_id'] )
@@ -1477,7 +1476,7 @@ class WP_Query {
                        $this->is_page = true;
                        $this->is_single = false;
                } else {
-               // Look for archive queries.  Dates, categories, authors, search, post type archives.
+               // Look for archive queries. Dates, categories, authors, search, post type archives.
 
                        if ( !empty($qv['s']) ) {
                                $this->is_search = true;
@@ -1625,7 +1624,7 @@ class WP_Query {
                }
 
                if ( '' != $qv['pagename'] ) {
-                       $this->queried_object =& get_page_by_path($qv['pagename']);
+                       $this->queried_object = get_page_by_path($qv['pagename']);
                        if ( !empty($this->queried_object) )
                                $this->queried_object_id = (int) $this->queried_object->ID;
                        else
@@ -2181,7 +2180,7 @@ class WP_Query {
                        if ( !empty($q['sentence']) ) {
                                $q['search_terms'] = array($q['s']);
                        } else {
-                               preg_match_all('/".*?("|$)|((?<=[\\s",+])|^)[^\\s",+]+/', $q['s'], $matches);
+                               preg_match_all('/".*?("|$)|((?<=[\r\n\t ",+])|^)[^\r\n\t ",+]+/', $q['s'], $matches);
                                $q['search_terms'] = array_map('_search_terms_tidy', $matches[0]);
                        }
                        $n = !empty($q['exact']) ? '' : '%';
@@ -2312,10 +2311,8 @@ class WP_Query {
 
                // MIME-Type stuff for attachment browsing
 
-               if ( isset($q['post_mime_type']) && '' != $q['post_mime_type'] ) {
-                       $table_alias = $post_status_join ? $wpdb->posts : '';
-                       $whichmimetype = wp_post_mime_type_where($q['post_mime_type'], $table_alias);
-               }
+               if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] )
+                       $whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
 
                $where .= $search . $whichauthor . $whichmimetype;
 
@@ -2346,6 +2343,7 @@ class WP_Query {
 
                                switch ( $orderby ) {
                                        case 'menu_order':
+                                               $orderby = "$wpdb->posts.menu_order";
                                                break;
                                        case 'ID':
                                                $orderby = "$wpdb->posts.ID";
@@ -2449,13 +2447,13 @@ class WP_Query {
                        }
                        if ( !empty($r_status) ) {
                                if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) )
-                                       $statuswheres[] = "($wpdb->posts.post_author = $user_ID " .  "AND (" . join( ' OR ', $r_status ) . "))";
+                                       $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_cap) )
-                                       $statuswheres[] = "($wpdb->posts.post_author = $user_ID " .  "AND (" . join( ' OR ', $p_status ) . "))";
+                                       $statuswheres[] = "($wpdb->posts.post_author = $user_ID " . "AND (" . join( ' OR ', $p_status ) . "))";
                                else
                                        $statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
                        }
@@ -2477,7 +2475,7 @@ class WP_Query {
                                $where .= " OR $wpdb->posts.post_status = '$state'";
                        }
 
-                       if ( is_admin() ) {
+                       if ( $this->is_admin ) {
                                // Add protected states that should show in the admin all list.
                                $admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
                                foreach ( (array) $admin_all_states as $state )
@@ -2510,18 +2508,16 @@ class WP_Query {
                // Paging
                if ( empty($q['nopaging']) && !$this->is_singular ) {
                        $page = absint($q['paged']);
-                       if ( empty($page) )
+                       if ( !$page )
                                $page = 1;
 
                        if ( empty($q['offset']) ) {
-                               $pgstrt = '';
                                $pgstrt = ($page - 1) * $q['posts_per_page'] . ', ';
-                               $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
                        } else { // we're ignoring $page and using 'offset'
                                $q['offset'] = absint($q['offset']);
                                $pgstrt = $q['offset'] . ', ';
-                               $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
                        }
+                       $limits = 'LIMIT ' . $pgstrt . $q['posts_per_page'];
                }
 
                // Comments feeds
@@ -2564,7 +2560,7 @@ class WP_Query {
 
                $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
 
-               // Apply post-paging filters on where and join.  Only plugins that
+               // 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 ) );
@@ -2581,10 +2577,10 @@ class WP_Query {
                                $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
                }
 
-               // Announce current selection parameters.  For use by caching plugins.
+               // Announce current selection parameters. For use by caching plugins.
                do_action( 'posts_selection', $where . $groupby . $orderby . $limits . $join );
 
-               // Filter again for the benefit of caching plugins.  Regular plugins should use the hooks above.
+               // 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 ) );
@@ -2609,9 +2605,11 @@ class WP_Query {
                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 ) );
+               $this->request = $old_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 ( 'ids' == $q['fields'] ) {
                        $this->posts = $wpdb->get_col($this->request);
@@ -2629,9 +2627,34 @@ class WP_Query {
                        return $r;
                }
 
-               $this->posts = $wpdb->get_results($this->request);
+               $split_the_query = ( $old_request == $this->request && "$wpdb->posts.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
+               $split_the_query = apply_filters( 'split_the_query', $split_the_query, $this );
+
+               if ( $split_the_query ) {
+                       // First get the IDs and then fill in the objects
 
-               // Raw results filter.  Prior to status checks.
+                       $this->request = "SELECT $found_rows $distinct $wpdb->posts.ID FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
+
+                       $this->request = apply_filters( 'posts_request_ids', $this->request, $this );
+
+                       $ids = $wpdb->get_col( $this->request );
+
+                       if ( $ids ) {
+                               $this->set_found_posts( $q, $limits );
+
+                               _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
+
+                               $this->posts = array_map( 'get_post', $ids );
+                       } else {
+                               $this->found_posts = $this->max_num_pages = 0;
+                               $this->posts = array();
+                       }
+               } else {
+                       $this->posts = $wpdb->get_results( $this->request );
+                       $this->set_found_posts( $q, $limits );
+               }
+
+               // Raw results filter. Prior to status checks.
                if ( !$q['suppress_filters'] )
                        $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
 
@@ -2648,13 +2671,6 @@ class WP_Query {
                        $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]);
@@ -2683,7 +2699,7 @@ class WP_Query {
                                }
                        }
 
-                       if ( $this->is_preview && current_user_can( $edit_cap, $this->posts[0]->ID ) )
+                       if ( $this->is_preview && $this->posts && current_user_can( $edit_cap, $this->posts[0]->ID ) )
                                $this->posts[0] = apply_filters_ref_array('the_preview', array( $this->posts[0], &$this ));
                }
 
@@ -2700,7 +2716,7 @@ class WP_Query {
                                        array_splice($this->posts, $i, 1);
                                        // Move to front, after other stickies
                                        array_splice($this->posts, $sticky_offset, 0, array($sticky_post));
-                                       // Increment the sticky offset.  The next sticky will be placed at this offset.
+                                       // Increment the sticky offset. The next sticky will be placed at this offset.
                                        $sticky_offset++;
                                        // Remove post from sticky posts array
                                        $offset = array_search($sticky_post->ID, $sticky_posts);
@@ -2757,6 +2773,18 @@ class WP_Query {
                return $this->posts;
        }
 
+       function set_found_posts( $q, $limits ) {
+               global $wpdb;
+
+               if ( $q['no_found_rows'] || empty( $limits ) )
+                       return;
+
+               $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
+               $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'] );
+       }
+
        /**
         * Set up the next post and iterate current post index.
         *
@@ -2929,7 +2957,7 @@ class WP_Query {
                if ( isset($this->queried_object) )
                        return $this->queried_object;
 
-               $this->queried_object = NULL;
+               $this->queried_object = null;
                $this->queried_object_id = 0;
 
                if ( $this->is_category || $this->is_tag || $this->is_tax ) {
@@ -2953,7 +2981,7 @@ class WP_Query {
                        $this->queried_object = get_post_type_object( $this->get('post_type') );
                } elseif ( $this->is_posts_page ) {
                        $page_for_posts = get_option('page_for_posts');
-                       $this->queried_object = get_page( $page_for_posts );
+                       $this->queried_object = get_page( $page_for_posts );
                        $this->queried_object_id = (int) $this->queried_object->ID;
                } elseif ( $this->is_singular && !is_null($this->post) ) {
                        $this->queried_object = $this->post;
@@ -3195,7 +3223,6 @@ class WP_Query {
                return (bool) $this->is_date;
        }
 
-
        /**
         * Is the query for a day archive?
         *
@@ -3593,4 +3620,3 @@ function setup_postdata($post) {
 
        return true;
 }
-?>