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

Mike Schinkel mikeschinkel at newclarity.net
Mon Jul 26 21:50:08 UTC 2010

On Jul 26, 2010, at 3:38 PM, John Blackbourn wrote:
> 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).

This might be it, but I'm not sure yet if that's the vision or not (cautiously optimistic):


>> 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.

Yeah.  Didn't expect it to be that easy. :)

>> 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.

Excellent.  Now if we can just capture that knowledge somewhere persistent and easy to find (like that plugin dev handbook?)...


More information about the wp-hackers mailing list