WordPress.org

Make WordPress Core


Ignore:
Timestamp:
09/20/2012 02:55:54 PM (9 years ago)
Author:
ryan
Message:
  • Avoid FOUND ROWS when no posts are found
  • Set post_count and found_posts for all 'fields' queries.
  • Set found_posts to post_count when limits are not used
  • Update phpdoc for $found_posts and set_found_posts()

Props SergeyBiryukov, wonderboymusic

fixes #14426

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/query.php

    r21890 r21928  
    980980
    981981    /**
    982      * Amount of posts if limit clause was not used.
     982     * The amount of found posts for the current query.
     983     *
     984     * If limit clause was not used, equals $post_count.
    983985     *
    984986     * @since 2.1.0
     
    26312633
    26322634        if ( 'ids' == $q['fields'] ) {
    2633             $this->posts = $wpdb->get_col($this->request);
     2635            $this->posts = $wpdb->get_col( $this->request );
     2636            $this->post_count = count( $this->posts );
     2637            $this->set_found_posts( $q, $limits );
    26342638
    26352639            return $this->posts;
     
    26372641
    26382642        if ( 'id=>parent' == $q['fields'] ) {
    2639             $this->posts = $wpdb->get_results($this->request);
     2643            $this->posts = $wpdb->get_results( $this->request );
     2644            $this->post_count = count( $this->posts );
     2645            $this->set_found_posts( $q, $limits );
    26402646
    26412647            $r = array();
     
    26592665
    26602666            if ( $ids ) {
     2667                $this->posts = $ids;
    26612668                $this->set_found_posts( $q, $limits );
    26622669                _prime_post_caches( $ids, $q['update_post_term_cache'], $q['update_post_meta_cache'] );
    2663                 $this->posts = $ids;
    26642670            } else {
    26652671                $this->posts = array();
    2666                 $this->found_posts = $this->max_num_pages = 0;
    26672672            }
    26682673        } else {
     
    27672772            $this->posts = apply_filters_ref_array('the_posts', array( $this->posts, &$this ) );
    27682773
    2769         $this->post_count = count($this->posts);
     2774        $this->post_count = count( $this->posts );
    27702775
    27712776        // Always sanitize
     
    27842789    }
    27852790
     2791    /**
     2792     * Set up the amount of found posts and the number of pages (if limit clause was used)
     2793     * for the current query.
     2794     *
     2795     * @since 3.5.0
     2796     * @access private
     2797     */
    27862798    function set_found_posts( $q, $limits ) {
    27872799        global $wpdb;
    27882800
    2789         if ( $q['no_found_rows'] || empty( $limits ) )
     2801        if ( $q['no_found_rows'] || ! $this->posts )
    27902802            return;
    27912803
    2792         $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
     2804        if ( ! empty( $limits ) )
     2805            $this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
     2806        else
     2807            $this->found_posts = count( $this->posts );
     2808
    27932809        $this->found_posts = apply_filters_ref_array( 'found_posts', array( $this->found_posts, &$this ) );
    27942810
    2795         $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
     2811        if ( ! empty( $limits ) )
     2812            $this->max_num_pages = ceil( $this->found_posts / $q['posts_per_page'] );
    27962813    }
    27972814
Note: See TracChangeset for help on using the changeset viewer.