Skip to content

Instantly share code, notes, and snippets.

@EdgeCaseBerg
Last active September 14, 2016 21:00
Show Gist options
  • Save EdgeCaseBerg/8787413 to your computer and use it in GitHub Desktop.
Save EdgeCaseBerg/8787413 to your computer and use it in GitHub Desktop.
Log SQL queries from WordPress
<?php
// in wordpress theme: functions.php add this in
function log_sql_queries($text_query){
/* //Uncomment me if you want a lot of info about where the sql query comes from and what action started it off
$traces = debug_backtrace();
$i = 0;
foreach ($traces as $tobj => $trace) {
if($trace['function'] == 'do_action'){
$args = $trace['args'];
}
error_log("TRACE:$i:" . $trace['function'] . print_r($args,1));
$i++;
}
*/
error_log("INFO:SQL:" . $text_query);
return $text_query;
}
add_filter( 'posts_request', 'log_sql_queries' );
//add_filter( 'query', 'log_sql_queries' ); //use this to see _everything_ , a lot of information including database selects that are likely served from the wp cache
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment