Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

It seems like half the tutorials in the codex and around the blogosphere use query_posts() and half use WP_Query. What's the deal?

share|improve this question

3 Answers

up vote 248 down vote accepted
  • query_posts() is overly simplistic and problematic way to modify 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 ofpre_get_posts hook, for this purpose. TL;DR don't use query_posts() ever;

  • get_posts() is very similar in usage and accepts same arguments (with some nuances, like different defaults), but returns array of posts, doesn't modify global variables and is safe to use anywhere;

  • WP_Query class powers both behind the scenes, but you can also create and work with own object of it. Bit more complex, less restrictions, also safe to use anywhere.

share|improve this answer
3  
@jjeaton query_posts() is tiny wrapper function for WP_Query, the only extra thing it does (as per flowchart) is overwriting global $wp_query – Rarst Aug 8 '11 at 15:39
5  
@jjeaton Replacing query_posts() with WP_Query will make no difference in performance, original page's query will still run because that is part of core load. Those queries will run even if your template file has no loop at all. – Rarst Aug 8 '11 at 17:15
28  
Can't get rid off the feeling that this the most genious and upvoted post on WPSE. Should be in Codex as well. – kaiser Sep 16 '11 at 0:03
1  
I'll just add my clearest description of the "performance of query_posts()" issue: Using query_posts() or WP_Query within a template file will have the same performnace cost: the query you just performed. The issue discussed in the codex article is that if you actually want to replace the query you should do so by filtering the original query_posts() with the 'parse_query' filter. That way you only have the one, original, desirable query, rather than doing a second query to awkwardly replace it. query_posts() is NEVER THE WAY!! NEVER! – Jeremy Clarke Apr 19 '12 at 22:24
2  
There's a freaking awesome explanation of query_posts written by John James Jacoby on the developer.wordpress.com blog that blows all of these answers out of the water. The main point: query_posts doesn't modify the main loop at all, it replaces it after it has already run. The best way to modify the main loop is through a pre_get_posts filter. developer.wordpress.com/2012/05/14/… – Dan Gayle Jun 9 '12 at 23:10
show 11 more comments

The basic difference is that query_posts() is really only for modifying the current Loop. Once you're done it's necessary to reset the loop and send it on its merry way. This method is also a little easier to understand, simply because your "query" is basically a URL string that you pass to the function, like so:

query_posts('meta_key=color&meta_value=blue'); 

On the other hand, wp_query is more of a general purpose tool, and is more like directly writing MySQL queries than query_posts() is. You can also use it anywhere (not just in the Loop) and it doesn't interfere with any currently running post queries.

I tend to use wp_query more often, as it happens. Really, it's going to come down to your specific case.

share|improve this answer

If I recall reading right, essentially "the loop" is doing WP_Queries in the core files, but in an easier to understand way.

share|improve this answer

protected by kaiser Feb 11 at 17:08

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

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