How To Get Data Through SQL Query In WordPress

In this article we will see how to get data from database using SQL Query in WordPress.

We are going to use get_results() function to execute the query.

$table_name = 'wp_users';
$results = $wpdb->get_results( " SELECT * FROM $table_name " );
echo "<pre>";
echo "</pre>";

Here in the above example I have gotten data of wp_users table using get_results function.

You can see I have used $wpdb variable.

$wpdp is a database access abstraction class.

Here I have added code and I even haven’t define $dpdb but still I am getting the results because I have added this code in my theme’s function file.

If you will not able to access the $wpdb then you need to assign this as global $wpdb;

If you have any difficulty you can reach out to me through comment.

Thank you.

Submit a Comment

Your email address will not be published. Required fields are marked *


Select Categories