Make WordPress Core

Ticket #10964: query.php.r17522.diff

File query.php.r17522.diff, 7.2 KB (added by kawauso, 14 years ago)

Patch against r17522, removes code block that broke paging

  • wp-includes/query.php

     
    878878        var $request;
    879879
    880880        /**
     881         * Get quick post database query.
     882         *
     883         * @since trunk
     884         * @access public
     885         * @var string
     886         */
     887        var $quick_request;
     888
     889        /**
    881890         * List of posts.
    882891         *
    883892         * @since 1.5.0
     
    18931902                $join = '';
    18941903                $search = '';
    18951904                $groupby = '';
    1896                 $fields = '';
     1905                $fields = "$wpdb->posts.ID"; // See #10964
     1906                $quick_fields = "$wpdb->posts.*";
    18971907                $post_status_join = false;
    18981908                $page = 1;
    18991909
     
    25142524
    25152525                $orderby = $q['orderby'];
    25162526
     2527                // Set up quick_* defaults from the standard placeholders, before filtering
     2528                $quick_distinct = $distinct;
     2529                $quick_where = $where;
     2530                $quick_join = $join;
     2531                $quick_groupby = $groupby;
     2532
    25172533                $pieces = array( 'where', 'groupby', 'join', 'orderby', 'distinct', 'fields', 'limits' );
     2534                $quick_pieces = array( 'quick_where', 'quick_groupby', 'quick_join', 'quick_orderby', 'quick_distinct', 'quick_fields', 'quick_limits' );
    25182535
    25192536                // Apply post-paging filters on where and join.  Only plugins that
    25202537                // manipulate paging queries should use these hooks.
     
    25312548                        $clauses = (array) apply_filters_ref_array( 'posts_clauses', array( compact( $pieces ), &$this ) );
    25322549                        foreach ( $pieces as $piece )
    25332550                                $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
     2551
     2552                        // And again for the quick clauses
     2553                        $clauses = (array) apply_filters_ref_array( 'posts_quick_clauses', array( compact( $quick_pieces ), &$this ) );
     2554                        foreach ( $quick_pieces as $piece )
     2555                                $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
    25342556                }
    25352557
    25362558                // Announce current selection parameters.  For use by caching plugins.
     
    25502572                        $clauses = (array) apply_filters_ref_array( 'posts_clauses_request', array( compact( $pieces ), &$this ) );
    25512573                        foreach ( $pieces as $piece )
    25522574                                $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
     2575
     2576                        // And again for the quick clauses
     2577                        $clauses = (array) apply_filters_ref_array( 'posts_quick_clauses_request', array( compact( $quick_pieces ), &$this ) );
     2578                        foreach ( $quick_pieces as $piece )
     2579                                $$piece = isset( $clauses[ $piece ] ) ? $clauses[ $piece ] : '';
    25532580                }
    25542581
    25552582                if ( ! empty($groupby) )
    25562583                        $groupby = 'GROUP BY ' . $groupby;
     2584                if ( ! empty($quick_groupby) )
     2585                        $quick_groupby = 'GROUP BY ' . $quick_groupby;
    25572586                if ( !empty( $orderby ) )
    25582587                        $orderby = 'ORDER BY ' . $orderby;
    25592588
     
    25612590                if ( !$q['no_found_rows'] && !empty($limits) )
    25622591                        $found_rows = 'SQL_CALC_FOUND_ROWS';
    25632592
    2564                 $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
    2565                 if ( !$q['suppress_filters'] )
    2566                         $this->request = apply_filters_ref_array('posts_request', array( $this->request, &$this ) );
     2593                if ( empty($limits) ) {
     2594                        // Do a direct query, as there is no benefit in fetching a huge load of IDs in an IN clause
     2595                        $this->request = " SELECT $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby";
     2596                        $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby";
    25672597
    2568                 if ( 'ids' == $q['fields'] ) {
    2569                         $this->posts = $wpdb->get_col($this->request);
     2598                        if ( 'ids' == $q['fields'] ) {
     2599                                $this->posts = $wpdb->get_col($this->request);
    25702600
    2571                         return $this->posts;
    2572                 }
     2601                                return $this->posts;
     2602                        }
    25732603
    2574                 if ( 'id=>parent' == $q['fields'] ) {
    2575                         $this->posts = $wpdb->get_results($this->request);
     2604                        if ( 'id=>parent' == $q['fields'] ) {
     2605                                $this->posts = $wpdb->get_results($this->request);
    25762606
    2577                         $r = array();
    2578                         foreach ( $this->posts as $post )
    2579                                 $r[ $post->ID ] = $post->post_parent;
     2607                                $r = array();
     2608                                foreach ( $this->posts as $post )
     2609                                        $r[ $post->ID ] = $post->post_parent;
    25802610
    2581                         return $r;
     2611                                return $r;
     2612                        }
     2613
     2614                        if ( !$q['suppress_filters'] ) {
     2615                                        $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this, false ) );
     2616                                        $this->quick_request = apply_filters_ref_array( 'posts_request', array( $this->quick_request, &$this, true ) );
     2617                        }
     2618
     2619                        $this->posts = $wpdb->get_results($this->quick_request);
     2620
     2621                        $this->found_posts = count($this->posts);
     2622                        $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
     2623                        $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
     2624                } else {
     2625                        $this->request = " SELECT $found_rows $distinct $fields FROM $wpdb->posts $join WHERE 1=1 $where $groupby $orderby $limits";
     2626                        if ( !$q['suppress_filters'] )
     2627                                        $this->request = apply_filters_ref_array( 'posts_request', array( $this->request, &$this, false ) );
     2628
     2629                        $post_ids = $wpdb->get_col($this->request);
     2630
     2631                        if ( empty($post_ids) ) {
     2632                                        $this->found_posts = 0;
     2633                                        $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
     2634                                        $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
     2635
     2636                                        $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1=1 $quick_where $quick_groupby $orderby";
     2637                                        if ( !$q['suppress_filters'] )
     2638                                                        $this->quick_request = apply_filters_ref_array('posts_request', array( $this->quick_request, &$this, true ) );
     2639
     2640                                        $this->posts = array(); // no point in querying, since there are no posts
     2641                        } else {
     2642                                        $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
     2643                                        $this->found_posts = $wpdb->get_var( $found_posts_query );
     2644                                        $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
     2645                                        $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
     2646
     2647                                        $post_ids = implode(',', $post_ids);
     2648                                        $this->quick_request = " SELECT $quick_distinct $quick_fields FROM $wpdb->posts $quick_join WHERE 1 = 1 $quick_where AND $wpdb->posts.ID IN ($post_ids) $quick_groupby ORDER BY FIELD( $wpdb->posts.ID, $post_ids ) ";
     2649
     2650                                        if ( !$q['suppress_filters'] )
     2651                                                        $this->quick_request = apply_filters_ref_array( 'posts_request', array( $this->quick_request, &$this, true ) );
     2652
     2653                                        $this->posts = $wpdb->get_results($this->quick_request);
     2654                        }
    25822655                }
    25832656
    2584                 $this->posts = $wpdb->get_results($this->request);
    2585 
    25862657                // Raw results filter.  Prior to status checks.
    25872658                if ( !$q['suppress_filters'] )
    25882659                        $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) );
     
    26002671                        $this->comment_count = count($this->comments);
    26012672                }
    26022673
    2603                 if ( !$q['no_found_rows'] && !empty($limits) ) {
    2604                         $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) );
    2605                         $this->found_posts = $wpdb->get_var( $found_posts_query );
    2606                         $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
    2607                         $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']);
    2608                 }
    2609 
    26102674                // Check post status to determine if post should be displayed.
    26112675                if ( !empty($this->posts) && ($this->is_single || $this->is_page) ) {
    26122676                        $status = get_post_status($this->posts[0]);