WordPress.org

Ready to get started?Download WordPress

Codex

Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Difference between revisions of "Function Reference/is main query"

(now calling the function like the page is documenting, instead of the method used in the function)
(Use the class method instead of the function. See http://core.trac.wordpress.org/ticket/23329)
Line 24: Line 24:
 
add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
 
add_action( 'pre_get_posts', 'foo_modify_query_exclude_category' );
 
function foo_modify_query_exclude_category( $query ) {
 
function foo_modify_query_exclude_category( $query ) {
if ( ! is_admin() && is_main_query() && ! $query->get( 'cat' ) )
+
if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) )
 
$query->set( 'cat', '-5' );
 
$query->set( 'cat', '-5' );
 
}
 
}

Revision as of 21:35, 30 January 2013

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