# Inspect the Main Query and Its SQL

> Lets administrators add ?wpide_query=1 to a front-end URL to print the main query's variables, the SQL it ran and the number of posts found at the bottom of the page.

- Type: PHP
- Runs: Frontend only
- Category: [Developer](https://wpide.com/library/category/developer/)
- Tags: debugging, wp-query, sql
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/debug-main-query-inspector/

## Code

```php
add_action( 'wp_footer', function () {
    if ( ! isset( $_GET['wpide_query'] ) || ! current_user_can( 'manage_options' ) ) {
        return;
    }

    global $wp_query;
    $vars = array_filter( (array) $wp_query->query_vars, function ( $value ) {
        return '' !== $value && array() !== $value && null !== $value && false !== $value;
    } );

    echo '<div style="margin:2rem;padding:1rem;background:#fff;color:#1d2327;border:1px solid #c3c4c7;font:13px/1.5 monospace;position:relative;z-index:99999;overflow:auto">';
    printf( '<p><strong>Found posts:</strong> %d &middot; <strong>Max pages:</strong> %d &middot; <strong>Queried object:</strong> %s</p>', (int) $wp_query->found_posts, (int) $wp_query->max_num_pages, esc_html( is_object( get_queried_object() ) ? get_class( get_queried_object() ) . ' #' . get_queried_object_id() : 'none' ) );
    echo '<p><strong>SQL</strong></p><pre style="white-space:pre-wrap">' . esc_html( (string) $wp_query->request ) . '</pre>';
    echo '<p><strong>Query vars</strong></p><pre style="white-space:pre-wrap">' . esc_html( print_r( $vars, true ) ) . '</pre></div>';
}, PHP_INT_MAX );
```

## Notes

Useful when a pre_get_posts change does not behave as expected.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Inspect the Main Query and Its SQL" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
