I’ve never been a fan of IDEs, complex debugging tools with breakpoints, variable watch lists and all that fancy stuff. var_dump()
and print_r()
have always been my best friends.
Recently I was playing around with the caching arguments in WP_Query
, trying to combine that with update_meta_cache()
while sticking wp_suspend_cache_addition()
somewhere there in the middle, and it quickly became a mess, so I wanted to know what queries am I actually running under the hood.
I came up with this little piece, which I think I’ll use more often from now on:
// Assuming SAVEQUERIES in set to true. $GLOBALS['wpdb']->queries = array(); // All the magic goes here var_dump( $GLOBALS['wpdb']->queries );
This gives you a nice list of SQL queries that were triggered only by that magic code in between. Works great when you need a quick sanity check on all those caching arguments, priming meta or term caches, splitting queries and whatnot.
Obviously it empties the initial set of queries, so anything in Debug Bar, Query Monitor, etc. will no longer be accurate.
What’s your favorite way to keep track of queries?
Запишу в свой блокнот :) Спасибо!
Query Monitor
I truly and heartfully recommend to you var_export instead of var_dump. It gives such a beautiful printout :)
var_export( $GLOBALS[‘wpdb’]->queries, true );
oops, true is if you want to get it into a string, otherwise (and default) false.
more info here: http://stackoverflow.com/questions/5039431/difference-between-var-dump-var-export-print-r