This is one of most information responses I’ve read on the web for ages about WordPress. Otto’s response gives a really good outline of how WordPress’s post meta caching works.
I’ve often wondered how many extra database queries will be required when doing a WP_Query() followed by a get_post_meta(). Otto makes it really clear that WordPress’s object cache will automatically be filled as part of the WP_Query itself.
“How the meta caching works for normal queries:
If the update_post_meta_cache parameter to the WP_Query is not set to false, then after the posts are retrieved from the DB, then the update_post_caches function will be called, which in turn calls update_postmeta_cache.
The update_postmeta_cache function is a wrapper for update_meta_cache, and it essentially calls a simple SELECT with all the ID’s of the posts retrieved. This will have it get all the postmeta, for all the posts in the query, and save that data in the object cache (using wp_cache_add).
When you do something like get_post_custom(), it’s checking that object cache first. So it’s not making extra queries to get the post meta at this point. If you’ve gotten the post in a WP_Query, then the meta is already in memory and it gets it straight from there.“
It’s also useful to consider for performance optimisation when you probably don’t want the postmeta for a query, that you can save time by setting the ‘update_post_meta_cache’ to false.
Read the full Stack Exchange answer at http://wordpress.stackexchange.com/a/39932