Make WordPress Core

Opened 6 years ago

Last modified 4 years ago

#46058 new defect (bug)

Extra database calls when use get_the_excerpt() and posts have images

Reported by: kg69design's profile kg69design Owned by:
Milestone: Awaiting Review Priority: normal
Severity: major Version: 5.0.3
Component: General Keywords: needs-patch
Focuses: Cc:

Description

To reproduce this issue:

  1. Install clear wordpress (5.0.3)
  2. Create post with image in post content. Assign it to one category (e.g. "uncategorized").
  3. Open this category page and check database calls in it (with get_num_queries()).
  4. Add one more post with image in it - and database calls will increase with every additional post (+2 queries for 1 additional post).

Info:

  1. This issue dissapear when you delete get_the_excerpt() from the category template.
  2. This issue does not exist when posts have no image in it.

The reason of this issue is in function get_the_excerpt():

  1. function get_the_excerpt() applies filter 'get_the_excerpt' in wp-includes/post-template.php:397
    <?php
    return apply_filters( 'get_the_excerpt', $post->post_excerpt, $post );
    
  1. we have 'wp_trim_excerpt' by default for 'get_the_excerpt' in "wp-includes/default-filters.php:172":
    <?php
    add_filter( 'get_the_excerpt', 'wp_trim_excerpt'  );
    


  1. function wp_trim_excerpt() applies filter 'the_content' in wp-includes/formatting.php:3314
    <?php
    $text = apply_filters( 'the_content', $text );
    
  1. we have 'wp_make_content_images_responsive' by default for 'the_content' in "wp-includes/default-filters.php:165":
    <?php
    add_filter( 'the_content', 'wp_make_content_images_responsive' );
    
  1. and lastly function wp_make_content_images_responsive() produce 2 extra database calls on every post in category-page (guess with wp_get_attachment_metadata) in wp-includes/media.php:1344
    <?php
    foreach ( $selected_images as $image => $attachment_id ) {
      $image_meta = wp_get_attachment_metadata( $attachment_id );
      $content = str_replace( $image, wp_image_add_srcset_and_sizes( $image, $image_meta, $attachment_id ), $content );
    }
    

Change History (1)

#1 @stuffradio
4 years ago

@kg69design I'm going to take a look into this soon but not today. I just made a note to see if I can replicate and then I will come up with a plan of action after that if I can replicate it.

Note: See TracTickets for help on using tickets.