Opened 8 years ago
Last modified 8 weeks ago
#46058 new defect (bug)
Extra database calls when use get_the_excerpt() and posts have images
| Reported by: | kg69design | Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Awaiting Review |
| Component: | General | Version: | 5.0.3 |
| Severity: | major | Keywords: | needs-patch |
| Cc: | Focuses: |
Description
To reproduce this issue:
- Install clear wordpress (5.0.3)
- Create post with image in post content. Assign it to one category (e.g. "uncategorized").
- Open this category page and check database calls in it (with get_num_queries()).
- Add one more post with image in it - and database calls will increase with every additional post (+2 queries for 1 additional post).
Info:
- This issue dissapear when you delete get_the_excerpt() from the category template.
- This issue does not exist when posts have no image in it.
The reason of this issue is in function get_the_excerpt():
- 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 );
- 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' );
- function wp_trim_excerpt() applies filter 'the_content' in wp-includes/formatting.php:3314
<?php $text = apply_filters( 'the_content', $text );
- 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' );
- 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 (3)
#2
@
6 months ago
Hi. I have attempted to reproduce this issue by following your steps exactly, but I was unable to replicate the extra database calls on WordPress 6.9 using the Twenty Nineteen classic theme.
Could you provide a bit more information?
- Current WordPress Version: Are you still able to reproduce this issue on the latest version of WordPress (6.9)?
- Theme: Which specific theme were you using when you originally encountered this?
- Environment Setup: Are you using any persistent object caching (e.g., Redis or Memcached) in your test environment?
- Plugins: Was this on a completely fresh install with zero active plugins, or were there any utility/developer plugins active?
#3
@
8 weeks ago
Replying to the original report: I can confirm this on 5.0.3, and it was fixed in 6.3.
I set up 5.0.3, 6.2.6, 6.3.7 and 6.9.8 side by side and counted get_num_queries() across
a category loop. Each post has its own attachment, an <img class="wp-image-ID"> in the
content, and an empty post_excerpt so the auto-generated path runs. Object cache flushed
before each measurement.
Queries added inside the loop:
version 1 post 3 posts 10 posts 5.0.3 2 6 20 6.2.6 1 3 10 6.3.7 0 0 0 6.9.8 0 0 0
Both conditions in the report hold on 5.0.3. Posts without images: 0 extra queries.
Images present but get_the_excerpt() removed: 0 extra queries.
The two queries per post on 5.0.3 are:
SELECT * FROM wp_posts WHERE ID = 76 LIMIT 1 SELECT post_id, meta_key, meta_value FROM wp_postmeta WHERE post_id IN (76)
The attachment post row plus its meta, from wp_get_attachment_metadata() and the
attachment URL lookup inside wp_calculate_image_srcset(). The post row lookup went away
before 6.2, which is why 6.2.6 shows +1 rather than +2. Same cause, smaller cost.
This was fixed in [55850] (#56588, milestone 6.3), which temporarily unhooks
wp_filter_content_tags() from the_content while wp_trim_excerpt() generates the
excerpt. That ticket was filed as a lazy-loading bug rather than a query-count one, so the
connection to this ticket was never made, but the root cause is the same and the fix
resolves what is described here.
Suggest closing as fixed.
Environment
- WordPress: 5.0.3, 6.2.6, 6.3.7, 6.9.8
- PHP: 8.3.6
- Server: none (PHP CLI)
- Database: mysqli (Server: MariaDB 10.11.14 / Client: mysqlnd 8.3.6)
- Theme: n/a, see note below
- MU-Plugins: None
- Plugins: None
Note on the harness: I called get_the_excerpt() directly inside a WP_Query loop rather
than rendering a full archive template, so no theme output is included in the counts. The
two controls above isolate the variables named in the report. 5.0.3 required small patches
to boot on PHP 8.3 (curly-brace string offsets in script-loader.php, shims for
get_magic_quotes_gpc() and get_magic_quotes_runtime()); nothing in the excerpt or media
path was touched.
@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.