Make WordPress Core

Ticket #10964: query.php.trunk.patch

File query.php.trunk.patch, 8.8 KB (added by rowanbeentje, 14 years ago)

Updated patch against trunk (r15490)

  • wp-includes/query.php

     
    16171617
    16181618                // First let's clear some variables
    16191619                $distinct = '';
     1620                $fields = "$wpdb->posts.ID"; // see #10964
     1621                $quick_fields = "$wpdb->posts.*";
    16201622                $whichcat = '';
    16211623                $whichauthor = '';
    16221624                $whichmimetype = '';
     1625                $search = '';
     1626                $join = '';
    16231627                $where = '';
     1628                $groupby = '';
     1629                $orderby = '';
    16241630                $limits = '';
    1625                 $join = '';
    1626                 $search = '';
    1627                 $groupby = '';
    1628                 $fields = "$wpdb->posts.*";
    16291631                $post_status_join = false;
    16301632                $page = 1;
    16311633
     
    23752377
    23762378                $orderby = $q['orderby'];
    23772379
     2380                // Set up quick_* defaults from the standard placeholders, before filtering
     2381                $quick_distinct = $distinct;
     2382                $quick_join = $join;
     2383                $quick_where = $where;
     2384                $quick_groupby = $groupby;
     2385
    23782386                // Apply post-paging filters on where and join.  Only plugins that
    23792387                // manipulate paging queries should use these hooks.
    23802388                if ( !$q['suppress_filters'] ) {
    2381                         $where          = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this ) );
    2382                         $groupby        = apply_filters_ref_array( 'posts_groupby',             array( $groupby, &$this ) );
    2383                         $join           = apply_filters_ref_array( 'posts_join_paged',  array( $join, &$this ) );
    2384                         $orderby        = apply_filters_ref_array( 'posts_orderby',             array( $orderby, &$this ) );
    2385                         $distinct       = apply_filters_ref_array( 'posts_distinct',    array( $distinct, &$this ) );
    2386                         $limits         = apply_filters_ref_array( 'post_limits',               array( $limits, &$this ) );
    2387                         $fields         = apply_filters_ref_array( 'posts_fields',              array( $fields, &$this ) );
     2389                        $distinct = apply_filters_ref_array( 'posts_distinct', array( $distinct, &$this, false ) );
     2390                        $quick_distinct = apply_filters_ref_array( 'posts_distinct', array( $quick_distinct, &$this, true ) );
     2391
     2392                        $fields = apply_filters_ref_array( 'posts_fields', array( $fields, &$this, false ) );
     2393                        $quick_fields = apply_filters_ref_array( 'posts_fields', array( $quick_fields, &$this, true ) );
     2394
     2395                        $join = apply_filters_ref_array( 'posts_join_paged', array( $join, &$this, false ) );
     2396                        $quick_join = apply_filters_ref_array( 'posts_join_paged', array( $quick_join, &$this, true ) );
     2397
     2398                        $where = apply_filters_ref_array( 'posts_where_paged', array( $where, &$this, false ) );
     2399                        $quick_where = apply_filters_ref_array( 'posts_where_paged', array( $quick_where, &$this, true ) );
     2400
     2401                        $groupby = apply_filters_ref_array( 'posts_groupby', array( $groupby, &$this, false ) );
     2402                        $quick_groupby = apply_filters_ref_array( 'posts_groupby', array( $quick_groupby, &$this, true ) );
     2403
     2404                        $orderby = apply_filters_ref_array( 'posts_orderby', array( $orderby, &$this ) );
     2405                        $limits = apply_filters_ref_array( 'post_limits', array( $limits, &$this ) );
    23882406                }
    23892407
    23902408                // Announce current selection parameters.  For use by caching plugins.
     
    23922410
    23932411                // Filter again for the benefit of caching plugins.  Regular plugins should use the hooks above.
    23942412                if ( !$q['suppress_filters'] ) {
    2395                         $where          = apply_filters_ref_array( 'posts_where_request',       array( $where, &$this ) );
    2396                         $groupby        = apply_filters_ref_array( 'posts_groupby_request',             array( $groupby, &$this ) );
    2397                         $join           = apply_filters_ref_array( 'posts_join_request',        array( $join, &$this ) );
    2398                         $orderby        = apply_filters_ref_array( 'posts_orderby_request',             array( $orderby, &$this ) );
    2399                         $distinct       = apply_filters_ref_array( 'posts_distinct_request',    array( $distinct, &$this ) );
    2400                         $fields         = apply_filters_ref_array( 'posts_fields_request',              array( $fields, &$this ) );
    2401                         $limits         = apply_filters_ref_array( 'post_limits_request',               array( $limits, &$this ) );
     2413                        $distinct = apply_filters_ref_array( 'posts_distinct_request', array( $distinct, &$this, false ) );
     2414                        $quick_distinct = apply_filters_ref_array( 'posts_distinct_request', array( $quick_distinct, &$this, true ) );
     2415
     2416                        $fields = apply_filters_ref_array( 'posts_fields_request', array( $fields, &$this, false ) );
     2417                        $quick_fields = apply_filters_ref_array( 'posts_fields_request', array( $quick_fields, &$this, true ) );
     2418
     2419                        $join = apply_filters_ref_array( 'posts_join_request', array( $join, &$this, false ) );
     2420                        $quick_join = apply_filters_ref_array( 'posts_join_request', array( $quick_join, &$this, true ) );
     2421
     2422                        $where = apply_filters_ref_array( 'posts_where_request', array( $where, &$this, false ) );
     2423                        $quick_where = apply_filters_ref_array( 'posts_where_request', array( $quick_where, &$this, true ) );
     2424
     2425                        $groupby = apply_filters_ref_array( 'posts_groupby_request', array( $groupby, &$this, false ) );
     2426                        $quick_groupby = apply_filters_ref_array( 'posts_groupby_request', array( $quick_groupby, &$this, true ) );
     2427
     2428                        $orderby = apply_filters_ref_array( 'posts_orderby_request', array( $orderby, &$this ) );
     2429                        $limits = apply_filters_ref_array( 'post_limits_request', array( $limits, &$this ) );
    24022430                }
    24032431
    24042432                if ( ! empty($groupby) )
    24052433                        $groupby = 'GROUP BY ' . $groupby;
     2434                if ( ! empty($quick_groupby) )
     2435                        $quick_groupby = 'GROUP BY ' . $quick_groupby;
    24062436                if ( !empty( $orderby ) )
    24072437                        $orderby = 'ORDER BY ' . $orderby;
    24082438                $found_rows = '';
    24092439                if ( !$q['no_found_rows'] && !empty($limits) )
    24102440                        $found_rows = 'SQL_CALC_FOUND_ROWS';
    24112441
    2412                 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
    2413                 if ( !$q['suppress_filters'] )
    2414                         $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) );
     2442                if ( empty($limits) ) {
     2443                        // do a direct query, as there is no benefit in fetching a huge load of IDs in an IN clause
     2444                        $this->request = " SELECT $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby";
     2445                        $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby";
    24152446
    2416                 $this->posts = $wpdb->get_results($this->request);
     2447                        if ( !$q['suppress_filters'] ) {
     2448                                $this->request = apply_filters('posts_request', $this->request, false);
     2449                                $this->quick_request = apply_filters('posts_request', $this->quick_request, true);
     2450                        }
     2451
     2452                        $this->posts = $wpdb->get_results($this->quick_request);
     2453
     2454                        $this->found_posts = count($this->posts);
     2455                        $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
     2456                        $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
     2457                } else {
     2458                        $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
     2459                        if ( !$q['suppress_filters'] )
     2460                                $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) );
     2461
     2462                        $post_ids = $wpdb->get_col($this->request);
     2463
     2464                        if ( !$post_ids ) {
     2465                                $this->found_posts = 0;
     2466                                $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
     2467                                $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
     2468
     2469                                $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby";
     2470                                if ( !$q['suppress_filters'] )
     2471                                        $this->quick_request = apply_filters('posts_request', $this->quick_request, true);
     2472                                $this->posts = array(); // no point in querying, since there are no posts
     2473                        } else {
     2474                                $found_posts_query = apply_filters( 'found_posts_query', 'SELECT FOUND_ROWS()' );
     2475                                $this->found_posts = $wpdb->get_var( $found_posts_query );
     2476                                $this->found_posts = apply_filters( 'found_posts', $this->found_posts );
     2477                                $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
     2478
     2479                                $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) . ") ";
     2480
     2481                                if ( !$q['suppress_filters'] )
     2482                                        $this->quick_request = apply_filters('posts_request', $this->quick_request, true);
     2483
     2484                                $this->posts = $wpdb->get_results($this->quick_request);
     2485                        }
     2486                }
     2487
    24172488                // Raw results filter.  Prior to status checks.
    24182489                if ( !$q['suppress_filters'] )
    24192490                        $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
     
    24312502                        $this->comment_count = count($this->comments);
    24322503                }
    24332504
    2434                 if ( !$q['no_found_rows'] && !empty($limits) ) {
    2435                         $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
    2436                         $this->found_posts = $wpdb->get_var( $found_posts_query );
    2437                         $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
    2438                         $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
    2439                 }
    2440 
    24412505                // Check post status to determine if post should be displayed.
    24422506                if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
    24432507                        $status = get_post_status($this->posts[0]);