When should you use WP_Query vs query_posts() vs get_posts()?

It seems like half the tutorials in the Codex and around the blogosphere use query_posts() and half use WP_Query.

They all do similar things, so when should I use one over the others?

Answer:

  • query_posts() is overly simplistic and a problematic way to modify the main query of a page by replacing it with new instance of the query. It is inefficient (re-runs SQL queries) and will outright fail in some circumstances (especially often when dealing with posts pagination). Any modern WP code should use more reliable methods, like making use of the pre_get_posts hook, for this purpose. TL;DR don’t use query_posts() ever.
  • get_posts() is very similar in usage and accepts the same arguments (with some nuances, like different defaults), but returns an array of posts, doesn’t modify global variables and is safe to use anywhere.
  • WP_Query is the class that powers both behind the scenes, but you can also create and work with your own instance of it. A bit more complex, fewer restrictions, also safe to use anywhere.
query functions

Leave a Comment

Your email address will not be published. Required fields are marked *