Title: is_main_query
Published: April 25, 2014
Last modified: February 24, 2026

---

# is_main_query(): bool

## In this article

 * [Description](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#description)
 * [Return](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#more-information)
    - [Usage](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#usage)
    - [Under the Hood](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#under-the-hood)
    - [Example](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#example)
 * [Source](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#user-contributed-notes)

[ Back to top](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#wp--skip-link--target)

Determines whether the query is the main query.

## 󠀁[Description](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#description)󠁿

For more information on this and similar theme functions, check out the [ Conditional Tags](https://developer.wordpress.org/themes/basics/conditional-tags/)
article in the Theme Developer Handbook.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#return)󠁿

 bool Whether the query is the main query.

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#more-information)󠁿

The `is_main_query()` function is a [conditional function](https://developer.wordpress.org/themes/basics/conditional-tags/)
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](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#usage)󠁿

    ```php
    if ( is_main_query() ) {
    // do stuff
    }
    ```

### 󠀁[Under the Hood](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#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()`

### 󠀁[Example](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#example)󠁿

    ```php
    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' );
    }
    ```

## 󠀁[Source](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#source)󠁿

    ```php
    function is_main_query() {
    	global $wp_query;

    	if ( ! isset( $wp_query ) ) {
    		_doing_it_wrong( __FUNCTION__, __( 'Conditional query tags do not work before the query is run. Before then, they always return false.' ), '6.1.0' );
    		return false;
    	}

    	if ( 'pre_get_posts' === current_filter() ) {
    		_doing_it_wrong(
    			__FUNCTION__,
    			sprintf(
    				/* translators: 1: pre_get_posts, 2: WP_Query->is_main_query(), 3: is_main_query(), 4: Documentation URL. */
    				__( 'In %1$s, use the %2$s method, not the %3$s function. See %4$s.' ),
    				'<code>pre_get_posts</code>',
    				'<code>WP_Query->is_main_query()</code>',
    				'<code>is_main_query()</code>',
    				__( 'https://developer.wordpress.org/reference/functions/is_main_query/' )
    			),
    			'3.7.0'
    		);
    	}

    	return $wp_query->is_main_query();
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-includes/query.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-includes/query.php#L902)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-includes/query.php#L902-L926)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#related)󠁿

| Uses | Description | 
| [WP_Query::is_main_query()](https://developer.wordpress.org/reference/classes/wp_query/is_main_query/)`wp-includes/class-wp-query.php` |

Determines whether the query is the main query.

  | 
| [current_filter()](https://developer.wordpress.org/reference/functions/current_filter/)`wp-includes/plugin.php` |

Retrieves the name of the current filter hook.

  | 
| [__()](https://developer.wordpress.org/reference/functions/__/)`wp-includes/l10n.php` |

Retrieves the translation of $text.

  | 
| [_doing_it_wrong()](https://developer.wordpress.org/reference/functions/_doing_it_wrong/)`wp-includes/functions.php` |

Marks something as being incorrectly called.

  |

[Show 2 more](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#)
[Show less](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#)

| Used by | Description | 
| [wp_get_loading_optimization_attributes()](https://developer.wordpress.org/reference/functions/wp_get_loading_optimization_attributes/)`wp-includes/media.php` |

Gets loading optimization attributes.

  | 
| [wp_get_loading_attr_default()](https://developer.wordpress.org/reference/functions/wp_get_loading_attr_default/)`wp-includes/deprecated.php` |

Gets the default value to use for a `loading` attribute on an element.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#changelog)󠁿

| Version | Description | 
| [3.3.0](https://developer.wordpress.org/reference/since/3.3.0/) | Introduced. |

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#user-contributed-notes)󠁿

 1.  [Skip to note 2 content](https://developer.wordpress.org/reference/functions/is_main_query/?output_format=md#comment-content-721)
 2.   [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/is_main_query/#comment-721)
 3. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fis_main_query%2F%23comment-721)
    Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fis_main_query%2F%23comment-721)
 4. **Example of using `WP_Query->is_main_query()` instead of `is_main_query()` (invalid)**
 5.     ```php
        /**
         * If the global query is for a category, exclude category 5.
         *
         * @param WP_Query $query Global WP_Query instance.
         */
        function wpdocs_modify_query_exclude_category( $query ) {
            if ( ! is_admin() && $query->is_main_query() && ! $query->get( 'cat' ) )
                $query->set( 'cat', '-5' );
        }
        add_action( 'pre_get_posts', 'wpdocs_modify_query_exclude_category' );
        ```
    
 6.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fis_main_query%2F%3Freplytocom%3D721%23feedback-editor-721)

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fis_main_query%2F)
before being able to contribute a note or feedback.