| @@ -78,9 +78,10 @@ | ||
| 78 | 78 | if ( ! is_object( $post ) ) { |
| 79 | 79 | return $images; |
| 80 | 80 | } |
| 81 | 81 | |
| 82 | - $thumbnail_id = get_post_thumbnail_id( $post->ID ); | |
| 82 | + // Pass the post object rather than its ID, so the post does not get re-fetched from the database. | |
| 83 | + $thumbnail_id = get_post_thumbnail_id( $post ); | |
| 83 | 84 | |
| 84 | 85 | if ( $thumbnail_id ) { |
| 85 | 86 | |
| 86 | 87 | $src = $this->get_absolute_url( $this->image_url( $thumbnail_id ) ); |
| @@ -129,8 +130,36 @@ | ||
| 129 | 130 | $images = $image_list; |
| 130 | 131 | } |
| 131 | 132 | |
| 132 | 133 | return $images; |
| 134 | + } | |
| 135 | + | |
| 136 | + /** | |
| 137 | + * Primes the meta caches of the featured images of the given posts. | |
| 138 | + * | |
| 139 | + * This parser reads each post's featured image file location from the attachment's | |
| 140 | + * meta individually; warming that meta cache in bulk avoids one query per post on | |
| 141 | + * setups without a persistent object cache. | |
| 142 | + * | |
| 143 | + * @param WP_Post[] $posts The posts to prime the featured-image caches for. | |
| 144 | + * | |
| 145 | + * @return void | |
| 146 | + */ | |
| 147 | + public function prime_thumbnail_caches( $posts ) { | |
| 148 | + | |
| 149 | + $thumbnail_ids = []; | |
| 150 | + | |
| 151 | + foreach ( $posts as $post ) { | |
| 152 | + $thumbnail_id = get_post_thumbnail_id( $post ); | |
| 153 | + | |
| 154 | + if ( $thumbnail_id ) { | |
| 155 | + $thumbnail_ids[] = $thumbnail_id; | |
| 156 | + } | |
| 157 | + } | |
| 158 | + | |
| 159 | + if ( ! empty( $thumbnail_ids ) ) { | |
| 160 | + update_meta_cache( 'post', array_unique( $thumbnail_ids ) ); | |
| 161 | + } | |
| 133 | 162 | } |
| 134 | 163 | |
| 135 | 164 | /** |
| 136 | 165 | * Get the images in the term description. |