I just noticed today that the documentation for query_posts() mentions some “disadvantages” of using query_posts for altering the main Loop, notably: ’causes additional SQL queries’.
This seems to imply that there’s another way / a better way. Obviously there’s get_posts() and WP_Query for secondary loops, but I don’t see them as addressing the “disadvantages” listed in the Codex documentation.
I can see that by waiting until you’re in the template to run query_posts, WordPress has already run a query once and this is now a second query that clobbers the first one (the first one being basically ignored). This definitely DOES sound inefficient (though perhaps not a big deal, who knows?)
My question is: is there an alternative to query_posts that does NOT add “additional SQL queries” or is the Codex documentation simply deceiving?
When I added the drawbacks to the Codex, I was mainly thinking of using the ‘request’ filter as an alternative to query_posts().
That filter is only run for the main query, so that solves the problem with ‘pre_get_posts’, which fires for every query.
The downside is that you don’t have access to query flags like is_single() etc.
Here’s one way you could get access to them, without actually doing the SQL queries: