WordPress.org

Codex

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

Function Reference/is main query

Description

The is_main_query() function is a conditional function that can be used to evaluate whether the current query (such as within the loop) is the "main" query (as opposed to a secondary query).

This function is most commonly used within hooks to distinguish WordPress' main query (for a page, post, or archive) from a custom/secondary query.

is_main_query() may be used with both front-end queries (theme templates, plugins, etc.), as well as admin queries. It will return true if the current query is the main query, and false if not.

Usage

<?php
if ( is_main_query() ) {
    // do stuff
}

Under the Hood

This function does not accept any parameters. Instead, it automatically compares the $wp_query object (i.e., the "current query") with the $wp_the_query object (the "main query")

This function is an alias for the method WP_Query::is_main_query(). In filter or action hook callbacks that are passed the WP_Query object, such as 'pre_get_posts', it is circular to call this function. Instead, directly call the passed object's method. For example, if your filter callback assigns the passed WP_Query object to $query, you would call the method like so: $query->is_main_query() See the example below for usage in a conditional if statement.

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, starting line 719.

WP_Query::is_main_query() is located in wp-includes/query.php, starting line 3816.

Related

Articles

Code Documentation

  • Class: WP_Query - Detailed Overview of class WP_Query
  • Class: WP_Comment_Query - Class for comment-related queries
  • Class: WP_User_Query - Class for user-related queries
  • Object: $wpdb - Overview on the use of the $wpdb object
  • Function: set_query_var()
  • Function: get_query_var()
  • Function: query_posts() - Create additional custom query
  • Function: get_post() - Take an ID of an item and return the records in the database for that article
  • Function: get_posts() - A specialized function that returns an array of items
  • Function: get_pages() - A specialized function that returns an array of pages
  • Function: have_posts() - A condition that determines whether the query returned an article
  • Function: the_post() - Used to automatically set the loop after a query
  • Function: rewind_posts() - Clears the current loop
  • Function: setup_postdata() - Sets the data for a single query result within a loop
  • Function: wp_reset_postdata() - Restores the previous query (usually after a loop within another loop)
  • Function: wp_reset_query()
  • Function: is_main_query() - Ensures that the query that is being changed is only the main query
  • Action Hook: pre_get_posts - Change WordPress queries before they are executed
  • Action Hook: the_post - Modify the post object after query
  • Filter Hook: found_posts - Changes the value of the object found_posts WP_Query

Conditional Tags