WordPress.org

Make WordPress Core

Ticket #15400: 15400-2-reduce-queries-to-number-of-posts-when-pagination-enable.patch

File 15400-2-reduce-queries-to-number-of-posts-when-pagination-enable.patch, 1.7 KB (added by westi, 7 years ago)

Another patch from http://www.google-melange.com/gci/task/show/google/gci2010/wordpress/t129162975592

  • wp-includes/comment.php

     
    792792                'trackback' => 'trackback',
    793793        );
    794794
    795         $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
    796795
    797         // Count comments older than this one
    798         $oldercoms = $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(comment_ID) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = 0 AND comment_approved = '1' AND comment_date_gmt < '%s'" . $comtypewhere, $comment->comment_post_ID, $comment->comment_date_gmt ) );
     796        if ( ($comsdatebypost = wp_cache_get( $comment->comment_post_ID, 'page_of_comment' )) == false ) {
     797                $comtypewhere = ( 'all' != $args['type'] && isset($allowedtypes[$args['type']]) ) ? " AND comment_type = '" . $allowedtypes[$args['type']] . "'" : '';
    799798
     799                // Count comments older than this one
     800                $comslist = $wpdb->get_results( $wpdb->prepare( "SELECT comment_date_gmt FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_parent = 0 AND comment_approved = '1'" . $comtypewhere, $comment->comment_post_ID ) );
     801
     802                $comsdatebypost = array();
     803                foreach ($comslist as $pagecom) {
     804                        $comsdatebypost[] =  strtotime( $pagecom->comment_date_gmt );
     805                }
     806                wp_cache_add( $comment->comment_post_ID, $comsdatebypost,  'page_of_comment' );
     807        }
     808       
     809        $oldercoms = 0;
     810       
     811        foreach($comsdatebypost as $comdate) {
     812                if( $comdate < strtotime($comment->comment_date_gmt) ) {
     813                        $oldercoms++;
     814                }
     815        }
     816
    800817        // No older comments? Then it's page #1.
    801818        if ( 0 == $oldercoms )
    802819                return 1;