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:
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:
If you enjoyed this post, make sure to subscribe to WPMayor’s RSS feed.