How to Read Data from the Database with wpdb in a Custom WordPress Plugin

Posted on 21st June 2023

When developing custom WordPress plugins, you will often need to read data from the WordPress database. In this article, we will show you how to use the WordPress database class (wpdb) to read data from the database in a custom plugin.

First, you need to include the WordPress database class (wpdb) in your plugin file. You can do this by adding the following line of code to your plugin file:

require_once( ABSPATH . ‘wp-admin/includes/class-wp-list-table.php’ );

Once you have included the WordPress database class in your plugin file, you can create an instance of the class by calling the wpdb constructor. The constructor takes two parameters: the database name and the database user.

$wpdb = new wpdb( ‘database_name’, ‘database_user’ );

After you have created an instance of the WordPress database class, you can start reading data from the database. The WordPress database class provides a number of methods for reading data from the database. We will cover some of the most commonly used methods in this article.

The first method we will cover is the get_results() method. This method can be used to retrieve a list of results from the database. The get_results() method takes two parameters: the SQL query and the output type.

The SQL query is the query that you want to run on the database. The output type is the type of data that you want the method to return. The output type can be one of four values: OBJECT, ARRAY_A, ARRAY_N, or OBJECT_K.

OBJECT – This will return the data as an object.
ARRAY_A – This will return the data as an associative array.
ARRAY_N – This will return the data as a numerically indexed array.
OBJECT_K – This will return the data as an associative array with the keys being the column names.

The get_results() method will return an empty array if no data is found.

$results = $wpdb->get_results( “SELECT * FROM table_name”, ARRAY_A );

The next method we will cover is the get_var() method. This method can be used to retrieve a single value from the database. The get_var() method takes two parameters: the SQL query and the column number.

The SQL query is the query that you want to run on the database. The column number is the number of the column that you want to retrieve the data from. The column number is zero-indexed, which means that the first column is column 0, the second column is column 1, and so on.

The get_var() method will return an empty string if no data is found.

$result = $wpdb->get_var( “SELECT column_name FROM table_name” );

The last method we will cover in this article is the get_row() method. This method can be used to retrieve a single row from the database. The get_row() method takes two parameters: the SQL query and the output type.

The SQL query is the query that you want to run on the database. The output type is the type of data that you want the method to return. The output type can be one of four values: OBJECT, ARRAY_A, ARRAY_N, or OBJECT_K.

OBJECT – This will return the data as an object.
ARRAY_A – This will return the data as an associative array.
ARRAY_N – This will return the data as a numerically indexed array.
OBJECT_K – This will return the data as an associative array with the keys being the column names.

The get_row() method will return an empty array if no data is found.

$result = $wpdb->get_row( “SELECT * FROM table_name”, ARRAY_A );

These are just a few of the methods that you can use to read data from the database in a custom WordPress plugin. For more information on the WordPress database class, please refer to the WordPress documentation.

If you’ve followed along with the article so far, you should have a pretty good understanding of how the wpdb class works. In this section, we’re going to learn how to use wpdb to read data from the database.

To read data from the database, we need to use the wpdb::get_results() method. This method accepts two parameters: the SQL query and an optional array of parameters.

The SQL query is a string that contains the SQL code that we want to execute. The optional array of parameters is an array of values that we want to substitute into the SQL query. These values will be replaced with question marks in the SQL query.

Here’s an example of how to use wpdb::get_results():

$sql = “SELECT * FROM wp_posts WHERE post_status = ‘publish'”;
$posts = $wpdb->get_results($sql);

In the example above, we’re using wpdb::get_results() to execute a SQL query that will select all of the published posts from the WordPress database. The $posts variable will contain an array of objects, each object representing a post.

We can then loop through the array of posts and display them on the screen:

foreach($posts as $post){
echo $post->post_title;
echo $post->post_content;
}

In the example above, we’re looping through the array of posts and displaying the title and content of each post.

You can also use wpdb::get_results() to execute a SQL query that returns a single row of data. To do this, you need to specify the $output parameter as an object.

Here’s an example of how to use wpdb::get_results() to return a single row of data:

$sql = “SELECT * FROM wp_posts WHERE post_status = ‘publish’ LIMIT 1”;
$post = $wpdb->get_results($sql, OBJECT);

In the example above, we’re using wpdb::get_results() to execute a SQL query that will select a single published post from the WordPress database. The $post variable will contain an object representing the post.

We can then access the data in the object:

echo $post->post_title;
echo $post->post_content;

In the example above, we’re accessing the title and content of the post.

If you want to return multiple rows of data as an array of objects, you can use the wpdb::get_results() method.

Here’s an example of how to use wpdb::get_results() to return multiple rows of data:

$sql = “SELECT * FROM wp_posts WHERE post_status = ‘publish'”;
$posts = $wpdb->get_results($sql);

In the example above, we’re using wpdb::get_results() to execute a SQL query that will select all of the published posts from the WordPress database. The $posts variable will contain an array of objects, each object representing a post.

We can then loop through the array of posts and display them on the screen:

foreach($posts as $post){
echo $post->post_title;
echo $post->post_content;
}

In the example above, we’re looping through the array of posts and displaying the title and content of each post.