Codex

Function Reference/is main query

Contents

Description

This function enables someone to hook into pre_get_posts and modify only the main query. This is a boolean function, meaning it returns either TRUE or FALSE. NOTE: admin screens also have a main query and this function can be used to detect it there.

Usage

<?php is_main_query(); ?>

Parameters

This tag does not accept any parameters.

Return Values

(boolean) 
True on success, false on failure.

Examples

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

Change Log

Since: 3.3

Source File

is_main_query() is located in wp-includes/query.php.

Related