13

I am working on wordpress which is new to me Now I have been provided with site completely designed in wp But the problem is I am unable to search the query for each functionality/page I found

$wpdb->get_results

used for getting result from database also this is not working

$wpdb->queries

Is there any way to print each query currently executing?.

Please Help.

3
  • Do you want to see the SQL query being executed? Nov 9, 2011 at 5:18
  • Not that I would discourage getting to know the internals, but this hints at another question whose answer would be very different. What problem are you trying to solve? Nov 9, 2011 at 14:55
  • best option is to enable SAVEQUERIES constant in wp-config.php and then using global variable $wpdb->queries in the template of interest.
    – maverick
    Dec 18, 2021 at 15:58

9 Answers 9

1

Debug bar is one of the recommended (by WordPress) plugins to install for theme and plugin development. I also use debug-bar-extender: http://wordpress.org/extend/plugins/debug-bar-extender/

15

For $wpdb->queries to work you need to configure in wp-config.php:

define('SAVEQUERIES', true);

This is highly not recommended for production (heavy performance hit) and so turned off by default.

1
8

I often do this when I need to check the current query:

add_action( 'wp_head', 'show_current_query' );

function show_current_query() {
    global $wp_query;

    if ( !isset( $_GET['q'] ) )
        return;
    echo '<textarea cols="50" rows="10">';
    print_r( $wp_query );
    echo '</textarea>';
}

To show the current query, just add ?q into the current URL.

This will show the current query (stored in global variable $wp_query), including the SQL query and all other query variables.

1
  • This is interesting, but it didn't show what I expected: the main query for the page. Instead, it showed the whole WordPress query, which I assume is converted to a MySQL query in the regular format at some point. May 24, 2020 at 0:58
1

I prefer Debug Bar.

Screenshot of Debug Bar running on WordPress 3.2.1

1

No need to install any plugin, you can use this:

$queried_object = get_queried_object();
var_dump( $queried_object );

Source: Codex

0

The global $wp_query contains everything about the current query, including the arguments, results, contents of the results, and the query itself.

$wp_query->request should do the job for you here.

0

You could also use the WPDB Profiling plugin that will display all SQL statements that were run to generate the page you're currently on, at the bottom of the page.

0

You can use the Wp Pear Debug plugin. It will show you a list of the queries executed in the page, the number of seconds those queries took to execute and the php function which initiated the query. Please note that you must save the settings at least once before it begins working.

0
<?php $wpdb->show_errors(); ?>  // use syntax just before your query 
<?php $wpdb->print_error(); ?>  // user after your query execution

Ie.

<?php $wpdb->show_errors();
      $id=$wpdb->update('table_name',$array,$condition);
      $wpdb->print_error();
?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.