1

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

1 Answer 1

3

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 the WP_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 repeatedly

When 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.

1
  • Well perfect, I've never seen much on the caching in Wordpress, and didn't know it caught post meta, that's really good to know. And I'll swap out query posts too, thanks!
    – Jess
    Jul 14, 2012 at 0:17

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.