wp-config.php » Save Database Queries for Analysis in WordPress

Save Database Queries for Analysis in WordPress

The SAVEQUERIES wp-config constant saves database queries to an array and We can use that array to display queries. The information saves each query, what function called it, and how long that query took to execute.

To enable SAVEQUERIES add this to the wp-config.php file:

define( 'SAVEQUERIES', true );

Then in the footer of your theme put this:

<?php
if ( current_user_can( 'administrator' ) ) {
    global $wpdb;
    echo "<pre>";
    print_r( $wpdb->queries );
    echo "</pre>";
}
?>

Open your website and in the footer you will notice queries.

2021 12 29 20 27 1024x342 - Save Database Queries for Analysis in WordPress

Note: This will have a performance impact on your site, so make sure to turn this off when you aren’t debugging.

See also  Use Regex with WP CLI to 🔎 Search & Replace in Database

Was this post helpful?

Leave a Comment

I enjoy constructive responses and professional comments to my posts, and invite anyone to comment or link to my site.

Recommended