[wp-hackers] Object terms not caching

John Blackbourn johnbillion+wp at gmail.com
Wed Sep 8 23:18:16 UTC 2010

Hi all,

In wp-includes/query.php at the end of WP_Query->get_posts() there's a
call to update_post_caches(). This fetches the post meta data and post
term data on all the posts that have just been retrieved and sticks
them in the object cache. This means when you do things like
get_post_meta() and get_the_tags() the data is already in the object
cache and no extra queries are needed.

I'm using a loop in WordPress which queries for posts of multiple post
types instead of just 'post'. When update_post_caches() is called it
fetches and caches the object meta data but it doesn't do the same for
object term data because there is more than one post type and it skips
over fetching object term data when the query is for more than one
post type (wp-includes/post.php line 4103).

This means any call to get_the_terms() fires off a query because the
object term data isn't cached. Furthermore, when terms are fetched in
this way they still don't get cached, so each call to get_the_terms()
fires off a query.

I've got thirty posts on my homepage and each of these posts is in
four custom taxonomies which I list using get_the_terms(). This means
it's doing 120 queries when it could be doing 1, and all because my
loop is querying for more than one post type.

I've patched my install so update_object_term_cache() is called for
each post type and it's cut all those queries out and there seems to
be no side effect.

So my question is, does anyone know if there is a reason why
update_post_caches() is set not to call update_object_term_cache()
when the post_type is an array (or set to 'any') instead of just one
post type?


More information about the wp-hackers mailing list