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

Andrew Nacin wp at andrewnacin.com
Mon Jul 26 16:28:30 UTC 2010

On Mon, Jul 26, 2010 at 11:13 AM, John Blackbourn
<johnbillion+wp at gmail.com<johnbillion%2Bwp at gmail.com>
> wrote:

> I suspect I'm missing something very simple here.
> I'm writing a plugin which adds a filter to pre_get_posts but the
> filter should only apply when we're dealing with the main WordPress
> loop (ie. the one which is constructed using the query string). There
> can potentially be several loops on any given page, and the main loop
> may not be the first loop on the page (so I can't apply the filter and
> then remove it at the end of the first loop).
> How can I determine if the current loop is the "main" loop constructed
> from the query string without having to touch the loop code in the
> template?

Contrary to the advice of many a tutorial, the pre_get_posts hook is almost
always the wrong hook to use here. Please look at the parse_request and
request hooks, and also a wp-hackers thread from a few days ago titled
"pre_get_posts problem".

More information about the wp-hackers mailing list