Sponsored Link

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.


<?php is_main_query(); ?>



This tag does not accept any parameters.




True on success, false on failure.

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


Since: 3.3



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


  • Class: WP_Query – Detailed overview of the WP_Query class
  • Class: $wpdb – Overview of using the $wpdb object
  • Function: get_query_var()
  • Function: query_posts() – Make additional custom queries
  • Function: get_posts() – A specialized function that returns an array of posts
  • Function: get_pages() – A specialized function that returns an array of pages
  • Function: have posts() – A conditional that determines if the query returned any posts
  • Function: the_post() – Used to automatically set up the loop after a query
  • Function: rewind_posts() – Resets the current loop
  • Function: setup_postdata() – Setup query data for individual results within a loop
  • Function: wp_reset_postdata() – Restores the previous query (usually after a loop-within-a-loop)
  • Function: wp_reset_query()
  • Function: is_main_query() – Ensures the query being changed is only the main query
  • Action Hook: pre_get_posts – Modify WordPress queries before they are executed
  • Filter Hook: found_posts – Modify the WP_Query object’s found_posts value
  • Tutorial: Displaying Posts Using a Custom Select Query
  • Tutorial: Making Advanced Taxonomy Queries
  • Tutorial: Making Custom Queries using Offset and Pagination
Sponsored Link