admin管理员组文章数量:1390547
I know I can use get_num_queries()
for the number of queries. However, how can I see what queries Wordpress actually makes? I've tried using $query but that didn't work.
I know I can use get_num_queries()
for the number of queries. However, how can I see what queries Wordpress actually makes? I've tried using $query but that didn't work.
4 Answers
Reset to default 7See this codex page.
in wp-config.php
:
define('SAVEQUERIES', true);
then in your template:
if (current_user_can('administrator')){
global $wpdb;
echo "<pre>";
print_r($wpdb->queries);
echo "</pre>";
}
or without the above SAVEQUERIES
, you can still see just the main query:
global $wp_query;
echo $wp_query->request;
or to see all of $wp_query
:
<pre>
<?php print_r($wp_query); ?>
</pre>
There is another low level way of finding out what queries have been made throughout a request which was very useful for me in order to identify what was being inserted into the database upon submitting a form.
Basically what I did was one database dump before submitting the form, another database dump after submitting the form, then a diff of the two.
Although it may look too primitive, the result was exactly what I needed, without having to install any plugins or modify any code or wp-config.php
.
The procedure I adopted was:
Get the form ready to submit, with all fields filled.
Do the first dump (change
user
,pass
anddbname
accordingly):mysqldump -uuser -ppass dbname --skip-opt > /tmp/before.sql
Submit the form.
Do the second dump (change
user
,pass
anddbname
accordingly):mysqldump -uuser -ppass dbname --skip-opt > /tmp/after.sql
Compare
before.sql
andafter.sql
, analyzing the result invim
:diff /tmp/before.sql /tmp/after.sql | vim -
Very useful for generating users migration scripts, for example.
Notes:
--skip-opt
generates a dump with one SQL instruction per line, so it's easier to see the changes when you rundiff
.| vim -
makesvim
read the output ofdiff
directly into its buffer.
Milo's answer is right on. I would add that to save you some trouble, download the WordPress Debug Bar plugin. This plugin will add a little bar to the bottom of your front end pages and it will capture important debugging information, including queries made during the request. For it to work you need to make sure that the constants WP_DEBUG
and SAVE_QUERIES
are set to true in your wp-config.php file.
Similar to above I highly recommend query monitor plugin: https://wordpress/plugins/query-monitor/ It has a lot of great features and really helps with local debugging.
More professional tools to use are different APMs, but they come paid. The best one I ever worked with was new rellic, but datadog APM works quite good well.
本文标签: wp queryview queries made
版权声明:本文标题:wp query - view queries made? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744701722a2620602.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论