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.


1 Answer 1


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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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