I’m building a custom index loop that just uses query_posts at the moment.
The issue is that inside The Loop, I have to then query for the post meta data (with get_post_meta), which I believe has to query the DB each time it’s called. Is there any way I can pull the meta data with the query_posts? I’d need most if not all of the meta data stored for each post.
Thanks,
Max
Actually, you’re a mistaken on a couple of points here.
1. Don’t use
query_posts()
This function is meant for modifying an existing query, not performing a standalone query. You should either use
get_posts()
or a new instance of theWP_Query()
class to perform your query for the custom loop.Here’s some further reading on
query_posts()
:2.
get_post_meta()
doesn’t hit the DB repeatedlyWhen you do run a post query, WordPress retrieves not only the post, but also the post meta. We use a built-in object cache to insulate the database from repeated requests for the same information. If you’re using a caching plugin on top of this, you can also receive the benefits of server-side tools like memcached.
You can read about the object cache in the Codex. Take a close look at the list of persistent cache plugins at the bottom of the page, not only will they make
get_post_meta()
calls lighter, they’ll speed up other elements of the site as well.