[wp-hackers] Applying a filter to the main loop only

Otto otto at ottodestruct.com
Mon Jul 26 19:42:29 UTC 2010

Hang on, I'm confused...

You guys keep mentioning the main "loop", but then go on to talk about
modifying the query for it and such. But the main post SELECT query is
run and done before the template is ever called, certainly before you
ever get to any "loop". It helps determine what template is called, in

So maybe I'm misunderstanding what you're talking about here, but the
first query is always the main one, unless it's modified by a plugin.
The Loop just displays posts from that already-run query.


On Mon, Jul 26, 2010 at 2:38 PM, John Blackbourn
<johnbillion+wp at gmail.com> wrote:
> On Mon, Jul 26, 2010 at 7:24 PM, Mike Schinkel
> <mikeschinkel at newclarity.net> wrote:
>> Whether John uses pre_get_posts or parse_request/request he still has the same problem; how to tell which is the "main" loop?  It's a great "Best Practices" question, actually.
>> As we use WordPress for a lot more patterns besides just blogging there are emerging lots of use-cases that the global nature of some of these hooks makes difficult. I've struggled with figuring out to write robust code because it seems most of the time I'm just inspecting "artifacts" (i.e. testing some object property for some value that I discover it has in my use-cases but unsure or unaware if it will always be true.) Coming up with a set of "well known solutions" for these main hooks would be rather helpful.
> Spot on, Mike. I find myself grepping through WordPress all the time
> to find the hooks and functions that I need and coming to my own
> conclusions about which hooks and filters are most appropriate. An
> archive of best practices aimed at plugin and theme developers (as
> mentioned in another thread) would save people a lot of time (and I'd
> be very willing to contribute to it).
>> For example, one solution that comes to mind for John is to use a static (or global) "flag" variable initially set to false or unset and then change it to flag that you've made it through the main query's loop.  But are there ever queries that may run before the main loop, and if so it will break this logic.
> Yeah your second point was my main problem; There are potentially
> loops that run both before and after the main loop so it's not just a
> case of assuming the first loop is the main loop as using a flag.
>> Back to John's question; how to determine if your hook is in the "main" loop; what artifacts does one inspect?
> I've determined that the 'request' filter is the best fit here as it's
> a filter on the main request parameters (ie. the results of parsing
> the query string) and therefore only affects the main loop. The
> 'parse_request' filter is similar but includes additional parameters
> that I didn't need in my scenario.
> John
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list