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?

link|improve this question

feedback

3 Answers

up vote 120 down vote accepted
  • query_posts() should be used in one and only case if you need to modify main query of page. It sets a lot of global variables and will lead to obscure and horrible bugs if used in any other place and for any other purpose;
  • get_posts() is very similar in mechanics and accepts same arguments, but returns array of posts, doesn't modify global variables and is safe to use anywhere;
  • WP_Query class power 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.

I want to stress that query_posts() wrongfully overused in many tutorials around. It is one of the most widespread bad practices.

link|improve this answer
(1) "and is safe to use anywhere" --> but do not use this for the MAIN loop. (2) remember to use global $query_string; before the line that has query_posts(); – edelwater Feb 19 '11 at 10:05
@scribu then again 'get_posts' will work although not advised: core.trac.wordpress.org/ticket/16545 – edelwater Feb 19 '11 at 10:27
1  
@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
5  
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 at 22:24
show 6 more comments
feedback

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.

link|improve this answer
feedback

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

link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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