WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a query that can only be done using straight SQL, but I need to loop through the returns as a set of posts. As I understand it, WP_Query automatically gets all the metadata for all the posts retrieved, and caches it for when get_post_meta is called. But since I have to use wpdb->query, I was wondering if there was anyway I could call another function after it with a set of like post ids, which would cache all their meta; this way I don't hit the database for every post in the loop.

Right now I am actually inner joining the fields I need from the post_meta, but this just seems like a bad (and messy) idea.


share|improve this question

I did a lot of digging into wordpress, and it looks like WP_Query ends up calling (indirectly) update_postmeta_cache($postIDs); which looks like it is exactly what I need.

Hope that helps somebody else out. In 3.5.1 it's located in wp-includes/post.php#2735.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.