故障排除 query_post(),如何获取通过函数调用执行的当前或最后一个 SQL 查询


Troubleshoting query_post(), how to get the current or last SQL query performed by function call

我的主题中有以下代码:

<?php 
    global $query_string;
    query_posts($query_string . "posts_per_page=".get_option('onthego_fromblog_random')."&ignore_sticky_posts=1&orderby=rand&cat=".get_catid(get_option('onthego_blog_cat'))); 
?>
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    <?php get_template_part('includes/fromblog_post'); ?>
<?php endwhile; endif; wp_reset_query(); ?>

但是它没有得到结果,我需要知道哪个是query_post()生成的SQL查询,以便在phpMyAdmin或其他工具中对其进行测试。如何?我查看了文档,但没有找到任何有用的内容

你不能在

一段时间内输入这段代码吗

die(creating query code...);

并转到使用它的页面?所以在你的情况下,它将是

die($query_string . "posts_per_page=".get_option('onthego_fromblog_random')."&ignore_sticky_posts=1&orderby=rand&cat=".get_catid(get_option('onthego_blog_cat')));

您可以复制此查询并将其粘贴到 PMA。

有一个

名为 Debugging in Wordpress 的 codex 页面,它讨论了使用 define('SAVEQUERIES', true); 来创建已执行查询数组。

或者,您可以通过将log = log_file_name添加到my.cnf文件中来将 MySQL 设置为记录查询。