WordPress.org

Make WordPress Core

Ticket #10964: 10964.diff

File 10964.diff, 6.9 KB (added by Denis-de-Bernardy, 9 years ago)
  • wp-includes/query.php

     
    15741574
    15751575                // First let's clear some variables
    15761576                $distinct = '';
     1577                $quick_distinct = '';
     1578                $fields = "$wpdb->posts.ID"; // see #10964
     1579                $quick_fields = "$wpdb->posts.*";
    15771580                $whichcat = '';
    15781581                $whichauthor = '';
    15791582                $whichmimetype = '';
     1583                $search = '';
     1584                $join = '';
     1585                $quick_join = '';
    15801586                $where = '';
     1587                $quick_where = '';
     1588                $groupby = '';
     1589                $quick_groupby = '';
     1590                $orderby = '';
    15811591                $limits = '';
    1582                 $join = '';
    1583                 $search = '';
    1584                 $groupby = '';
    1585                 $fields = "$wpdb->posts.*";
    15861592                $post_status_join = false;
    15871593                $page = 1;
    15881594
     
    22282234                // Apply post-paging filters on where and join.  Only plugins that
    22292235                // manipulate paging queries should use these hooks.
    22302236                if ( !$q['suppress_filters'] ) {
    2231                         $where = apply_filters('posts_where_paged', $where);
    2232                         $groupby = apply_filters('posts_groupby', $groupby);
    2233                         $join = apply_filters('posts_join_paged', $join);
     2237                        $distinct = apply_filters('posts_distinct', $distinct, false);
     2238                        $quick_distinct = apply_filters('posts_distinct', $quick_distinct, true);
     2239
     2240                        $fields = apply_filters('posts_fields', $fields, false);
     2241                        $quick_fields = apply_filters('posts_fields', $quick_fields, true);
     2242
     2243                        $join = apply_filters('posts_join_paged', $join, false);
     2244                        $quick_join = apply_filters('posts_join_paged', $quick_join, true);
     2245
     2246                        $where = apply_filters('posts_where_paged', $where, false);
     2247                        $quick_where = apply_filters('posts_where_paged', $quick_where, true);
     2248
     2249                        $groupby = apply_filters('posts_groupby', $groupby, true);
     2250                        $quick_groupby = apply_filters('posts_groupby', $quick_groupby, true);
     2251
    22342252                        $orderby = apply_filters('posts_orderby', $orderby);
    2235                         $distinct = apply_filters('posts_distinct', $distinct);
    22362253                        $limits = apply_filters( 'post_limits', $limits );
    2237 
    2238                         $fields = apply_filters('posts_fields', $fields);
    22392254                }
    22402255
    22412256                // Announce current selection parameters.  For use by caching plugins.
     
    22432258
    22442259                // Filter again for the benefit of caching plugins.  Regular plugins should use the hooks above.
    22452260                if ( !$q['suppress_filters'] ) {
    2246                         $where = apply_filters('posts_where_request', $where);
    2247                         $groupby = apply_filters('posts_groupby_request', $groupby);
    2248                         $join = apply_filters('posts_join_request', $join);
     2261                        $distinct = apply_filters('posts_distinct_request', $distinct, false);
     2262                        $quick_distinct = apply_filters('posts_distinct_request', $quick_distinct, true);
     2263
     2264                        $fields = apply_filters('posts_fields_request', $fields, false);
     2265                        $quick_fields = apply_filters('posts_fields_request', $quick_fields, true);
     2266
     2267                        $join = apply_filters('posts_join_request', $join, false);
     2268                        $quick_join = apply_filters('posts_join_request', $quick_join, true);
     2269
     2270                        $where = apply_filters('posts_where_request', $where, false);
     2271                        $quick_where = apply_filters('posts_where_request', $quick_where, true);
     2272
     2273                        $groupby = apply_filters('posts_groupby_request', $groupby, false);
     2274                        $quick_groupby = apply_filters('posts_groupby_request', $quick_groupby, true);
     2275
    22492276                        $orderby = apply_filters('posts_orderby_request', $orderby);
    2250                         $distinct = apply_filters('posts_distinct_request', $distinct);
    2251                         $fields = apply_filters('posts_fields_request', $fields);
    22522277                        $limits = apply_filters( 'post_limits_request', $limits );
    22532278                }
    22542279
     
    22602285                if ( !empty($limits) )
    22612286                        $found_rows = 'SQL_CALC_FOUND_ROWS';
    22622287
    2263                 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
    2264                 if ( !$q['suppress_filters'] )
    2265                         $this->request = apply_filters('posts_request', $this->request);
     2288                if ( empty($limits) ) {
     2289                        // do a direct query, as there is no benefit in fetching a huge load of IDs in an IN clause
     2290                        $this->request = " SELECT $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby";
     2291                        $this->quick_request = " SELECT $distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby";
     2292                       
     2293                        if ( !$q['suppress_filters'] ) {
     2294                                $this->request = apply_filters('posts_request', $this->request, false);
     2295                                $this->quick_request = apply_filters('posts_request', $this->quick_request, true);
     2296                        }
     2297                       
     2298                       
     2299                        $this->posts = $wpdb->get_results($this->quick_request);
     2300                       
     2301                        $this->found_posts = count($this->posts);
     2302                        $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
     2303                        $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
     2304                } else {
     2305                        $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits ";
     2306                        if ( !$q['suppress_filters'] )
     2307                                $this->request = apply_filters('posts_request', $this->request, false);
     2308                       
     2309                        $post_ids = $wpdb->get_col($this->request);
     2310                       
     2311                        if ( !$post_ids ) {
     2312                                $this->found_posts = 0;
     2313                                $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
     2314                                $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
    22662315
    2267                 $this->posts = $wpdb->get_results($this->request);
     2316                                $this->quick_request = " SELECT $distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby";
     2317                                if ( !$q['suppress_filters'] )
     2318                                        $this->quick_request = apply_filters('posts_request', $this->quick_request, true);
     2319                                $this->posts = array(); // no point in querying, since there are no posts
     2320                        } else {
     2321                                $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
     2322                                $this->found_posts = $wpdb->get_var( $found_posts_query );
     2323                                $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
     2324                                $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
     2325                               
     2326                                $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 ";
     2327                                if ( !$q['suppress_filters'] )
     2328                                        $this->quick_request = apply_filters('posts_request', $this->quick_request, true);
     2329                               
     2330                                $this->posts = $wpdb->get_results($this->quick_request);
     2331                        }
     2332                }
     2333               
    22682334                // Raw results filter.  Prior to status checks.
    22692335                if ( !$q['suppress_filters'] )
    22702336                        $this->posts = apply_filters('posts_results', $this->posts);
     
    22822348                        $this->comment_count = count($this->comments);
    22832349                }
    22842350
    2285                 if ( !empty($limits) ) {
    2286                         $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
    2287                         $this->found_posts = $wpdb->get_var( $found_posts_query );
    2288                         $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
    2289                         $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
    2290                 }
    2291 
    22922351                // Check post status to determine if post should be displayed.
    22932352                if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
    22942353                        $status = get_post_status($this->posts[0]);