One of my most upvoted answers at WordPress Stack Exchange is for question about WP_Query vs query_posts() vs get_posts(). I think no other WordPress issue caused that many bugs and frustrations and this is one of the very few areas where I see even most experienced developers misstep.
So I’ve been coming back to my answer from time to time, because I didn’t feel entirely content with it. Telling people what to do relies on their memory (meh) and positive reinforcement. Unfortunately most people encounter way more crappy WordPress code around than good one. Mistakes in usage of query functions just get copied and used again and again for years.
At first I wanted to do extended table comparison between functions but I lost the fight with markup. My second take was grabbing Balsamiq Mockups and (after two or three takes that failed miserably) I came up with following flowchart.
I put it under Creative Commons Attribution-ShareAlike so I can also post it at WPSE, so feel free to grab a copy as well.
And do tell if I got any details wrong. :)
Klemen #
Heiner #
Rarst #
Rakesh Kumar #
Rarst #
Charleston #
Rarst #
finchamgroves #
Rarst #
$some_variable = new WP_Query();
or$some_variable = new WP_Query($some_query_arguments);
Because they probably want to change main Loop temporarily for some reason, but don't have a clue how to use query_posts() and wp_reset_query(). Which they should be using, instead of that contraption.finchamgroves #
Rarst #
finchamgroves #
Rarst #
Marcko #
kaiser #
Rarst #
kaiser #
Rarst #
Chris Olbekson #
Vladimir #
get_posts
orwp_query
I just preferget_posts
and not fully understood it is betterRarst #