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

If you purchase through a link on our site, we may earn a commission. Learn more.

I recently came across a fantastic diagram by Rarst showing the difference between WP_Query(), query_posts() and get_posts(). As you may know, these are all ways of retrieving posts in WordPress, but they are not all equal.

Table of Contents
WP Engine High Performance Hosting
BionicWP Hosting

Update: A better way of thinking about the loop can be seen in the following talk and slides by Andrew Nacin, plus a written explanation by John James Jacoby if you are more inclined to understand things that way:

Nacin: http://wordpress.tv/2012/06/15/andrew-nacin-wp_query/

Jacoby: http://developer.wordpress.com/2012/05/14/querying-posts-without-query_posts/

Thanks goes to Konstantin Kovshenin for pointing this out (see comment below)


I recently came across a diagram in a WordPress Stackexchange answer by Rarst showing the difference between WP_Query(), query_posts() and get_posts(). As you may know, these are all ways of retrieving posts in WordPress, but they are not all equal.

Here’s the difference:

  • query_posts() might be used in one and only case if you need to modify main query of page (for which there are better and more reliable methods to accomplish, over simplistic approach of this function). 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.

Here’s another post which is worth a read:

http://digwp.com/2011/05/loops/

If you enjoyed this post, make sure to subscribe to WPMayor’s RSS feed.

Jean Galea

Jean Galea is an investor, entrepreneur, and blogger. He is the founder of WP Mayor, the plugins WP RSS Aggregator and Spotlight, as well as the Mastermind.fm podcast. His personal blog can be found at jeangalea.com.

Discover more from our archives ↓

Popular articles ↓

6 Responses

  1. Hi, Jean Galea

    Nice post! Now, I know the difference. For me, it turned out better using WP_Query. Thanks 😀

  2. That answer is originally two years old and nowadays people are eager to poke holes in it, so I did new editorial pass there. 🙂 Also why push again query_posts() is now thankfully much more prominent, newer techniques does not really invalidate basic loop mechanics in template stage (which is all my answer and diagram were essentially about).

    Also if you are copying content in full from WPSE answer please make it clear under terms of the license (CC-BY-SA) and consider following attribution requirements

  3. query_posts() might be used in one and only case if you need to modify main query of page (for which there are better and more reliable methods to accomplish, over simplistic approach of this function).

    Wrong 🙂 All three methods (query_posts, new WP_Query and get_posts) will issue a new query, so all three must be used in one and only one case: a secondary loop. If you want to modify the primary (main) loop you should do that before the query runs and the two best places for that are: the pre_get_posts action, and the request filter.

    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

    It really just sets only one — the $wp_query global 🙂 It actually destroys it first though! It destroys the link to $wp_the_query and assigns a new WP_Query to $wp_query, and wp_reset_query restores back the link to $wp_the_query. It’s quite simple and straightforward if you read the wp-includes/query.php file.

    Actually I’m tired of repeating this over and over again, so why don’t we all just go and watch Nacin’s talk about WP_Query which has been hanging on the homepage of WordPress.tv for ages now:

    And if you ask me, query_posts should be deprecated. Without warning.

    ~ K

    1. +1 Konstantin!

      better to create one’s own content than republishing others content without really understanding it…

    2. Thanks for the enlightening comment Konstantin, I’m going to update the post with this new info. I’ve taken a look at the Codex query_posts page and it indeed mentions the same thing:

      I’ve either somehow not noticed it before or it’s a fairly new update in the Codex.

Share Your Thoughts

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

Claim Your Free Website Tip 👇

Leave your name, email and website URL below to receive one actionable improvement tip tailored for your website within the next 24 hours.

"They identified areas for improvement that we had not previously considered." - Elliot

By providing your information, you'll also be subscribing to our weekly newsletter packed with exclusive content and insights. You can unsubscribe at any time with just one click.