Skip to content

how to see sql queried by wordpress when opening a page

For learning purpose, it's meaningful to looking at sql sentences queried by Wordpress. But how to do this? Here is the answer. First, edit the wp-config.php file, just add one line of code as below:

php
define('SAVEQUERIES', true);

Then, find the common footer.php file of your Wordpress theme, add the following code before the ending </body> html tag (attention: here we only allow administrator to see these sql sentences, taking security into consideration):

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

That's all you need to do. You can now open a webpage and see corresponding sql sentences at the footer of the page. Thanks for reading!

天上的神明与星辰,人间的艺术和真纯,我们所敬畏和热爱的,莫过于此。