WordPress.org

Make WordPress Core

Ticket #18536: query.patch

File query.patch, 2.5 KB (added by cheald, 22 months ago)

Query performance patch

  • wp-includes/query.php

     
    19401940                $fields = ''; 
    19411941                $post_status_join = false; 
    19421942                $page = 1; 
     1943                $expand_ids = false; 
    19431944 
    19441945                if ( isset( $q['caller_get_posts'] ) ) { 
    19451946                        _deprecated_argument( 'WP_Query', '3.1', __( '"caller_get_posts" is deprecated. Use "ignore_sticky_posts" instead.' ) ); 
     
    20262027                                $fields = "$wpdb->posts.ID, $wpdb->posts.post_parent"; 
    20272028                                break; 
    20282029                        default: 
    2029                                 $fields = "$wpdb->posts.*"; 
     2030                                $fields = "$wpdb->posts.ID"; 
     2031                                $expand_ids = true; 
    20302032                } 
    20312033 
    20322034                // If a month is specified in the querystring, load that month 
     
    26062608 
    26072609                if ( 'ids' == $q['fields'] ) { 
    26082610                        $this->posts = $wpdb->get_col($this->request); 
    2609  
    26102611                        return $this->posts; 
    2611                 } 
     2612                } elseif($expand_ids) { 
     2613                        $ids = $wpdb->get_col($this->request); 
    26122614 
    2613                 if ( 'id=>parent' == $q['fields'] ) { 
     2615                        if ( !$q['no_found_rows'] && !empty($limits) ) { 
     2616                                $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ); 
     2617                                $this->found_posts = $wpdb->get_var( $found_posts_query ); 
     2618                                $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) ); 
     2619                                $this->max_num_pages = ceil($this->found_posts / $q['posts_per_page']); 
     2620                        } 
     2621 
     2622                        $query = sprintf("SELECT $wpdb->posts.* FROM $wpdb->posts WHERE ID IN (%s) $orderby", join(",", $ids)); 
     2623                        $this->posts = $wpdb->get_results($query);                       
     2624                } elseif ( 'id=>parent' == $q['fields'] ) { 
    26142625                        $this->posts = $wpdb->get_results($this->request); 
    26152626 
    26162627                        $r = array(); 
     
    26182629                                $r[ $post->ID ] = $post->post_parent; 
    26192630 
    26202631                        return $r; 
     2632                } else { 
     2633                        $this->posts = $wpdb->get_results($this->request); 
    26212634                } 
    26222635 
    2623                 $this->posts = $wpdb->get_results($this->request); 
    2624  
    26252636                // Raw results filter.  Prior to status checks. 
    26262637                if ( !$q['suppress_filters'] ) 
    26272638                        $this->posts = apply_filters_ref_array('posts_results', array( $this->posts, &$this ) ); 
     
    26392650                        $this->comment_count = count($this->comments); 
    26402651                } 
    26412652 
    2642                 if ( !$q['no_found_rows'] && !empty($limits) ) { 
     2653                if ( !$q['no_found_rows'] && !empty($limits) && !$expand_ids ) { 
    26432654                        $found_posts_query = apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ); 
    26442655                        $this->found_posts = $wpdb->get_var( $found_posts_query ); 
    26452656                        $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );