WordPress.org

Make WordPress Core

Ticket #8958: reduce_attach_query.8958.diff

File reduce_attach_query.8958.diff, 1.4 KB (added by robinmarshall, 9 years ago)

Patch as described in comment 14

  • wp-includes/rewrite.php

    ### Eclipse Workspace Patch 1.0
    #P wordpress-dev
     
    794794
    795795                if ( !$posts )
    796796                        return array( array(), array() );
    797 
     797               
     798                // get the set of posts with an attachment to reduce the number of queries inside the loop
     799                $all_attachments = $wpdb->get_results( "SELECT post_parent FROM $wpdb->posts WHERE post_type = 'attachment'" );
     800               
    798801                foreach ($posts as $id => $post) {
     802                               
    799803                        // URL => page name
    800804                        $uri = get_page_uri($id);
    801                         $attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ));
    802                         if ( $attachments ) {
    803                                 foreach ( $attachments as $attachment ) {
    804                                         $attach_uri = get_page_uri($attachment->ID);
    805                                         $page_attachment_uris[$attach_uri] = $attachment->ID;
     805                        if (in_array( $id, $all_attachments )) {
     806                                $attachments = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_name, post_parent FROM $wpdb->posts WHERE post_type = 'attachment' AND post_parent = %d", $id ));
     807                                if ( $attachments ) {
     808                                        foreach ( $attachments as $attachment ) {
     809                                                $attach_uri = get_page_uri($attachment->ID);
     810                                                $page_attachment_uris[$attach_uri] = $attachment->ID;
     811                                        }
    806812                                }
    807813                        }
    808814