How can I display a query in a page?

I’m new with WordPress development and I trying to display a query result from the wp_postmeta table.

Does anyone know how to that? Do I have to connect with the MySQL with mysql_connect() or does WP have a built-in function for that?

Thanks!

1

Answers


  1. Pim
    0 Votes

    You can use the $wpdb object

    // Print last SQL query string
    $wpdb->last_query
    

    or:

    <?php
      if (current_user_can('administrator')){
        global $wpdb;
        echo "<pre>";
        print_r($wpdb->queries);
        echo "</pre>";
      }//Lists all the queries executed on your page
    ?>
    

    You might have to set define('SAVEQUERIES', true); in wp-config.php

Please signup or login to answer this question.

Signup for an account and start participating in our site today!




Social Signup

You can also login with your Facebook, Twitter or Linkedin account

Log in with Facebook