[wp-trac] [WordPress Trac] #15447: Cache Post Thumbnails

WordPress Trac wp-trac at lists.automattic.com
Wed Nov 17 10:03:51 UTC 2010


#15447: Cache Post Thumbnails
-----------------------------+----------------------------------------------
 Reporter:  TheDeadMedic     |       Owner:                 
     Type:  feature request  |      Status:  new            
 Priority:  normal           |   Milestone:  Awaiting Review
Component:  General          |     Version:                 
 Severity:  normal           |    Keywords:                 
-----------------------------+----------------------------------------------
 Database queries can rapidly increase when using the post thumbnails
 feature, especially whilst in the loop (on average, 2 additional queries
 '''per post''').

 Suggest caching all thumbnails for the current query - will usually drop
 to just two additional DB queries '''in total''', regardless of number of
 posts.

 {{{

 // only run if feature supported by theme

 $thumbnail_IDs = array();
 foreach ($posts as $post)
         $thumbnail_IDs[] = get_post_thumbnail_id($post->ID);

 if ($thumbnail_IDs) {
         $thumbnail_IDs = array_filter( array_unique($thumbnail_IDs) );
         get_posts(array(
                 'update_post_term_cache' => false,
                 'include' => $thumbnail_IDs,
                 'post_type' => 'attachment',
                 'post_status' => 'inherit',
                 'nopaging' => true
         ));
 }
 }}}

 Obviously the code above is pure example - when, and where, this is best
 placed I'm not sure. Perhaps hooked to the 'wp' action, so it only runs
 for the main query?

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/15447>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list