WordPress.org

Make WordPress Core

Ticket #10964: query.php.2.9.2.patch

File query.php.2.9.2.patch, 7.4 KB (added by rowanbeentje, 8 years ago)

Updated patch against 2.9.2

  • wp-includes/query.php

     
    15551555
    15561556                // First let's clear some variables
    15571557                $distinct = '';
     1558                $fields = "$wpdb->posts.ID"; // see #10964
     1559                $quick_fields = "$wpdb->posts.*";
    15581560                $whichcat = '';
    15591561                $whichauthor = '';
    15601562                $whichmimetype = '';
     1563                $search = '';
     1564                $join = '';
    15611565                $where = '';
     1566                $groupby = '';
     1567                $orderby = '';
    15621568                $limits = '';
    1563                 $join = '';
    1564                 $search = '';
    1565                 $groupby = '';
    1566                 $fields = "$wpdb->posts.*";
    15671569                $post_status_join = false;
    15681570                $page = 1;
    15691571
     
    22072209
    22082210                $orderby = $q['orderby'];
    22092211
     2212                // Set up quick_* defaults from the standard placeholders, before filtering
     2213                $quick_distinct = $distinct;
     2214                $quick_join = $join;
     2215                $quick_where = $where;
     2216                $quick_groupby = $groupby;
     2217
    22102218                // Apply post-paging filters on where and join.  Only plugins that
    22112219                // manipulate paging queries should use these hooks.
    22122220                if ( !$q['suppress_filters'] ) {
    2213                         $where = apply_filters('posts_where_paged', $where);
    2214                         $groupby = apply_filters('posts_groupby', $groupby);
    2215                         $join = apply_filters('posts_join_paged', $join);
     2221                        $distinct = apply_filters('posts_distinct', $distinct, false);
     2222                        $quick_distinct = apply_filters('posts_distinct', $quick_distinct, true);
     2223
     2224                        $fields = apply_filters('posts_fields', $fields, false);
     2225                        $quick_fields = apply_filters('posts_fields', $quick_fields, true);
     2226
     2227                        $join = apply_filters('posts_join_paged', $join, false);
     2228                        $quick_join = apply_filters('posts_join_paged', $quick_join, true);
     2229
     2230                        $where = apply_filters('posts_where_paged', $where, false);
     2231                        $quick_where = apply_filters('posts_where_paged', $quick_where, true);
     2232
     2233                        $groupby = apply_filters('posts_groupby', $groupby, false);
     2234                        $quick_groupby = apply_filters('posts_groupby', $quick_groupby, true);
     2235
    22162236                        $orderby = apply_filters('posts_orderby', $orderby);
    2217                         $distinct = apply_filters('posts_distinct', $distinct);
    2218                         $limits = apply_filters( 'post_limits', $limits );
    2219 
    2220                         $fields = apply_filters('posts_fields', $fields);
     2237                        $limits = apply_filters( 'post_limits', $limits);
    22212238                }
    22222239
    22232240                // Announce current selection parameters.  For use by caching plugins.
     
    22252242
    22262243                // Filter again for the benefit of caching plugins.  Regular plugins should use the hooks above.
    22272244                if ( !$q['suppress_filters'] ) {
    2228                         $where = apply_filters('posts_where_request', $where);
    2229                         $groupby = apply_filters('posts_groupby_request', $groupby);
    2230                         $join = apply_filters('posts_join_request', $join);
     2245                        $distinct = apply_filters('posts_distinct_request', $distinct, false);
     2246                        $quick_distinct = apply_filters('posts_distinct_request', $quick_distinct, true);
     2247
     2248                        $fields = apply_filters('posts_fields_request', $fields, false);
     2249                        $quick_fields = apply_filters('posts_fields_request', $quick_fields, true);
     2250
     2251                        $join = apply_filters('posts_join_request', $join, false);
     2252                        $quick_join = apply_filters('posts_join_request', $quick_join, true);
     2253
     2254                        $where = apply_filters('posts_where_request', $where, false);
     2255                        $quick_where = apply_filters('posts_where_request', $quick_where, true);
     2256
     2257                        $groupby = apply_filters('posts_groupby_request', $groupby, false);
     2258                        $quick_groupby = apply_filters('posts_groupby_request', $quick_groupby, true);
     2259
    22312260                        $orderby = apply_filters('posts_orderby_request', $orderby);
    2232                         $distinct = apply_filters('posts_distinct_request', $distinct);
    2233                         $fields = apply_filters('posts_fields_request', $fields);
    2234                         $limits = apply_filters( 'post_limits_request', $limits );
     2261                        $limits = apply_filters( 'post_limits_request', $limits);
    22352262                }
    22362263
    22372264                if ( ! empty($groupby) )
    22382265                        $groupby = 'GROUP BY ' . $groupby;
     2266                if ( ! empty($quick_groupby) )
     2267                        $quick_groupby = 'GROUP BY ' . $quick_groupby;
    22392268                if ( !empty( $orderby ) )
    22402269                        $orderby = 'ORDER BY ' . $orderby;
    22412270                $found_rows = '';
    22422271                if ( !empty($limits) )
    22432272                        $found_rows = 'SQL_CALC_FOUND_ROWS';
    22442273
    2245                 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
    2246                 if ( !$q['suppress_filters'] )
    2247                         $this->request = apply_filters('posts_request', $this->request);
     2274                if ( empty($limits) ) {
     2275                        // do a direct query, as there is no benefit in fetching a huge load of IDs in an IN clause
     2276                        $this->request = " SELECT $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby";
     2277                        $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby";
    22482278
    2249                 $this->posts = $wpdb->get_results($this->request);
     2279                        if ( !$q['suppress_filters'] ) {
     2280                                $this->request = apply_filters('posts_request', $this->request, false);
     2281                                $this->quick_request = apply_filters('posts_request', $this->quick_request, true);
     2282                        }
     2283
     2284                        $this->posts = $wpdb->get_results($this->quick_request);
     2285
     2286                        $this->found_posts = count($this->posts);
     2287                        $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
     2288                        $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
     2289                } else {
     2290                        $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
     2291                        if ( !$q['suppress_filters'] )
     2292                                $this->request = apply_filters('posts_request', $this->request);
     2293
     2294                        $post_ids = $wpdb->get_col($this->request);
     2295
     2296                        if ( !$post_ids ) {
     2297                                $this->found_posts = 0;
     2298                                $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
     2299                                $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
     2300
     2301                                $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby";
     2302                                if ( !$q['suppress_filters'] )
     2303                                        $this->quick_request = apply_filters('posts_request', $this->quick_request, true);
     2304                                $this->posts = array(); // no point in querying, since there are no posts
     2305                        } else {
     2306                                $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
     2307                                $this->found_posts = $wpdb->get_var( $found_posts_query );
     2308                                $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
     2309                                $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
     2310
     2311                                $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) . ") ";
     2312
     2313                                if ( !$q['suppress_filters'] )
     2314                                        $this->quick_request = apply_filters('posts_request', $this->quick_request, true);
     2315
     2316                                $this->posts = $wpdb->get_results($this->quick_request);
     2317                        }
     2318                }
     2319
    22502320                // Raw results filter.  Prior to status checks.
    22512321                if ( !$q['suppress_filters'] )
    22522322                        $this->posts = apply_filters('posts_results', $this->posts);
     
    22642334                        $this->comment_count = count($this->comments);
    22652335                }
    22662336
    2267                 if ( !empty($limits) ) {
    2268                         $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
    2269                         $this->found_posts = $wpdb->get_var( $found_posts_query );
    2270                         $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
    2271                         $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
    2272                 }
    2273 
    22742337                // Check post status to determine if post should be displayed.
    22752338                if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
    22762339                        $status = get_post_status($this->posts[0]);