Identify which loop you are hooking into; primary or secondary?

Just a quick update that a new method is_main_query() has been introduced in WP 3.3.

Example:

add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
function foo_modify_query_exclude_category( $query ) {
    if ( $query->is_main_query() && ! $query->get( 'cat' ) )
    $query->set( 'cat', '-5' );
}

Resources:

Leave a Comment