WordPress.org

Make WordPress Core

Ticket #10964: 10964.4.diff

File 10964.4.diff, 6.9 KB (added by ryan, 8 years ago)

10964.diff tweaked to fix queries without limits

  • wp-includes/query.php

     
    15731573
    15741574                // First let's clear some variables
    15751575                $distinct = '';
     1576                $quick_distinct = '';
     1577                $fields = "$wpdb->posts.ID"; // see #10964
     1578                $quick_fields = "$wpdb->posts.*";
    15761579                $whichcat = '';
    15771580                $whichauthor = '';
    15781581                $whichmimetype = '';
     1582                $search = '';
     1583                $join = '';
     1584                $quick_join = '';
    15791585                $where = '';
     1586                $quick_where = '';
     1587                $groupby = '';
     1588                $quick_groupby = '';
     1589                $orderby = '';
    15801590                $limits = '';
    1581                 $join = '';
    1582                 $search = '';
    1583                 $groupby = '';
    1584                 $fields = "$wpdb->posts.*";
    15851591                $post_status_join = false;
    15861592                $page = 1;
    15871593
     
    22332239                // Apply post-paging filters on where and join.  Only plugins that
    22342240                // manipulate paging queries should use these hooks.
    22352241                if ( !$q['suppress_filters'] ) {
    2236                         $where = apply_filters('posts_where_paged', $where);
    2237                         $groupby = apply_filters('posts_groupby', $groupby);
    2238                         $join = apply_filters('posts_join_paged', $join);
     2242                        $distinct = apply_filters('posts_distinct', $distinct, false);
     2243                        $quick_distinct = apply_filters('posts_distinct', $quick_distinct, true);
     2244
     2245                        $fields = apply_filters('posts_fields', $fields, false);
     2246                        $quick_fields = apply_filters('posts_fields', $quick_fields, true);
     2247
     2248                        $join = apply_filters('posts_join_paged', $join, false);
     2249                        $quick_join = apply_filters('posts_join_paged', $quick_join, true);
     2250
     2251                        $where = apply_filters('posts_where_paged', $where, false);
     2252                        $quick_where = apply_filters('posts_where_paged', $quick_where, true);
     2253
     2254                        $groupby = apply_filters('posts_groupby', $groupby, true);
     2255                        $quick_groupby = apply_filters('posts_groupby', $quick_groupby, true);
     2256
    22392257                        $orderby = apply_filters('posts_orderby', $orderby);
    2240                         $distinct = apply_filters('posts_distinct', $distinct);
    22412258                        $limits = apply_filters( 'post_limits', $limits );
    2242 
    2243                         $fields = apply_filters('posts_fields', $fields);
    22442259                }
    22452260
    22462261                // Announce current selection parameters.  For use by caching plugins.
     
    22482263
    22492264                // Filter again for the benefit of caching plugins.  Regular plugins should use the hooks above.
    22502265                if ( !$q['suppress_filters'] ) {
    2251                         $where = apply_filters('posts_where_request', $where);
    2252                         $groupby = apply_filters('posts_groupby_request', $groupby);
    2253                         $join = apply_filters('posts_join_request', $join);
     2266                        $distinct = apply_filters('posts_distinct_request', $distinct, false);
     2267                        $quick_distinct = apply_filters('posts_distinct_request', $quick_distinct, true);
     2268
     2269                        $fields = apply_filters('posts_fields_request', $fields, false);
     2270                        $quick_fields = apply_filters('posts_fields_request', $quick_fields, true);
     2271
     2272                        $join = apply_filters('posts_join_request', $join, false);
     2273                        $quick_join = apply_filters('posts_join_request', $quick_join, true);
     2274
     2275                        $where = apply_filters('posts_where_request', $where, false);
     2276                        $quick_where = apply_filters('posts_where_request', $quick_where, true);
     2277
     2278                        $groupby = apply_filters('posts_groupby_request', $groupby, false);
     2279                        $quick_groupby = apply_filters('posts_groupby_request', $quick_groupby, true);
     2280
    22542281                        $orderby = apply_filters('posts_orderby_request', $orderby);
    2255                         $distinct = apply_filters('posts_distinct_request', $distinct);
    2256                         $fields = apply_filters('posts_fields_request', $fields);
    22572282                        $limits = apply_filters( 'post_limits_request', $limits );
    22582283                }
    22592284
     
    22652290                if ( !empty($limits) )
    22662291                        $found_rows = 'SQL_CALC_FOUND_ROWS';
    22672292
    2268                 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
    2269                 if ( !$q['suppress_filters'] )
    2270                         $this->request = apply_filters('posts_request', $this->request);
     2293                if ( empty($limits) ) {
     2294                        // do a direct query, as there is no benefit in fetching a huge load of IDs in an IN clause
     2295                        $this->request = " SELECT $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby";
     2296                        $this->quick_request = " SELECT $distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $where $quick_groupby $orderby";
    22712297
    2272                 $this->posts = $wpdb->get_results($this->request);
     2298                        if ( !$q['suppress_filters'] ) {
     2299                                $this->request = apply_filters('posts_request', $this->request, false);
     2300                                $this->quick_request = apply_filters('posts_request', $this->quick_request, true);
     2301                        }
     2302
     2303                        $this->posts = $wpdb->get_results($this->quick_request);
     2304
     2305                        $this->found_posts = count($this->posts);
     2306                        $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
     2307                        $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
     2308                } else {
     2309                        $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits ";
     2310                        if ( !$q['suppress_filters'] )
     2311                                $this->request = apply_filters('posts_request', $this->request, false);
     2312
     2313                        $post_ids = $wpdb->get_col($this->request);
     2314
     2315                        if ( !$post_ids ) {
     2316                                $this->found_posts = 0;
     2317                                $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
     2318                                $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
     2319
     2320                                $this->quick_request = " SELECT $distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby";
     2321                                if ( !$q['suppress_filters'] )
     2322                                        $this->quick_request = apply_filters('posts_request', $this->quick_request, true);
     2323                                $this->posts = array(); // no point in querying, since there are no posts
     2324                        } else {
     2325                                $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
     2326                                $this->found_posts = $wpdb->get_var( $found_posts_query );
     2327                                $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
     2328                                $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
     2329
     2330                                $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 $orderby ";
     2331                                if ( !$q['suppress_filters'] )
     2332                                        $this->quick_request = apply_filters('posts_request', $this->quick_request, true);
     2333
     2334                                $this->posts = $wpdb->get_results($this->quick_request);
     2335                        }
     2336                }
     2337
    22732338                // Raw results filter.  Prior to status checks.
    22742339                if ( !$q['suppress_filters'] )
    22752340                        $this->posts = apply_filters('posts_results', $this->posts);
     
    22872352                        $this->comment_count = count($this->comments);
    22882353                }
    22892354
    2290                 if ( !empty($limits) ) {
    2291                         $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
    2292                         $this->found_posts = $wpdb->get_var( $found_posts_query );
    2293                         $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
    2294                         $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
    2295                 }
    2296 
    22972355                // Check post status to determine if post should be displayed.
    22982356                if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
    22992357                        $status = get_post_status($this->posts[0]);