How to Create Custom Queries for Post Meta using wpdb in WordPress Plugin

Posted on 19th June 2023

In WordPress, a plugin is a piece of software containing a group of functions that can be added to a WordPress website. They can extend functionality or add new features to your WordPress websites. WordPress plugins are written in the PHP programming language and integrate seamlessly with WordPress. In this tutorial, we will show you how to create custom queries for post meta using wpdb in your WordPress plugin.

What is wpdb?

wpdb is the WordPress Database Abstraction layer. It is used to access a WordPress database. It provides an object-oriented interface, which makes it easy to use in WordPress plugins. wpdb can be used to run custom queries, fetch data from the database, and update data in the database.

Creating a Custom Query

To create a custom query, you first need to create a WordPress plugin. You can do this by creating a new file in your WordPress plugin directory. For this tutorial, we will be creating a plugin called “My Plugin”.

In your “My Plugin” directory, create a new file called “my-plugin.php”. Paste the following code into your “my-plugin.php” file:


<?php
/*
Plugin Name: My Plugin
Plugin URI: https://example.com/my-plugin
Description: A plugin to demonstrate how to create custom queries for post meta using wpdb.
Version: 1.0
Author: John Doe
Author URI: https://example.com
License: GPLv2 or later
*/
?>

This is the minimum code required to create a WordPress plugin. We will be adding to this code later on.

Next, you need to create a function to run your custom query. Paste the following code into your “my-plugin.php” file:


function my_plugin_custom_query() {
// your code goes here
}

Replace “your code goes here” with the following code:


global $wpdb;

$post_meta_table = $wpdb->prefix . "postmeta";

$query = "SELECT post_id, meta_key, meta_value FROM $post_meta_table WHERE meta_key = 'my_meta_key'";

$results = $wpdb->get_results( $query );

foreach ( $results as $result ) {
echo $result->post_id . " - " . $result->meta_key . " - " . $result->meta_value;
echo "<br />";
}

This code will query the “postmeta” table in your WordPress database for all posts that have a meta key of “my_meta_key”. It will then loop through the results and echo the post ID, meta key, and meta value for each post.

You can now call the “my_plugin_custom_query” function from anywhere in your WordPress plugin. For example, you could add the following code to your “my-plugin.php” file:


function my_plugin_custom_query() {
global $wpdb;

$post_meta_table = $wpdb->prefix . "postmeta";

$query = "SELECT post_id, meta_key, meta_value FROM $post_meta_table WHERE meta_key = 'my_meta_key'";

$results = $wpdb->get_results( $query );

foreach ( $results as $result ) {
echo $result->post_id . " - " . $result->meta_key . " - " . $result->meta_value;
echo "<br />";
}
}

add_action( 'init', 'my_plugin_custom_query' );

This code will call the “my_plugin_custom_query” function when WordPress initializes. This is just one example of how you can call the function. You can call it from any WordPress hook or filter.

Fetching Data from the Database

In the previous section, we showed you how to run a custom query and loop through the results. In this section, we will show you how to fetch data from the database and store it in a variable.

Paste the following code into your “my-plugin.php” file:


function my_plugin_fetch_data() {
global $wpdb;

$post_meta_table = $wpdb->prefix . "postmeta";

$query = "SELECT post_id, meta_key, meta_value FROM $post_meta_table WHERE meta_key = 'my_meta_key'";

$results = $wpdb->get_results( $query );

$data = array();

foreach ( $results as $result ) {
$data[] = array(
'post_id' => $result->post_id,
'meta_key' => $result->meta_key,
'meta_value' => $result->meta_value
);
}

return $data;
}

This code will fetch all posts that have a meta key of “my_meta_key” and store them in an array. The array will contain the post ID, meta key, and meta value for each post. You can then use the “my_plugin_fetch_data” function to retrieve the data from the database.

For example, you could add the following code to your “my-plugin.php” file:


function my_plugin_fetch_data() {
global $wpdb;

$post_meta_table = $wpdb->prefix . "postmeta";

$query = "SELECT post_id, meta_key, meta_value FROM $post_meta_table WHERE meta_key = 'my_meta_key'";

$results = $wpdb->get_results( $query );

$data = array();

foreach ( $results as $result ) {
$data[] = array(
'post_id' => $result->post_id,
'meta_key' => $result->meta_key,
'meta_value' => $result->meta_value
);
}

return $data;
}

$data = my_plugin_fetch_data();

foreach ( $data as $d ) {
echo $d['post_id'] . " - " . $d['meta_key'] . " - " . $d['meta_value'];
echo "<br />";
}

This code will call the “my_plugin_fetch_data” function and store the results in a variable called “$data”. It will then loop through the “$data” variable and echo the post ID, meta key, and meta value for each post.

Updating Data in the Database

In the previous section, we showed you how to fetch data from the database. In this section, we will show you how to update data in the database.

Paste the following code into your “my-plugin.php” file:


function my_plugin_update_data( $post_id, $meta_key, $meta_value ) {
global $wpdb;

$post_meta_table = $wpdb->prefix . "postmeta";

$query = $wpdb->prepare( "UPDATE $post_meta_table SET meta_value = %s WHERE post_id = %d AND meta_key = %s", $meta_value, $post_id, $

If you're not familiar with SQL, then custom queries for post meta can seem a bit daunting. However, with a little bit of knowledge, you can easily create custom queries for post meta using the WordPress database class, wpdb.

The first thing you need to do is create a new file called "custom-queries.php" in your plugin's directory. Next, you need to include the WordPress database class, which you can do by adding the following line of code to your custom-queries.php file:

get_results( "SELECT * FROM $wpdb->postmeta WHERE post_id = $post_id" );

foreach ( $results as $result ) {

// do something with the results

}

This example will retrieve all of the post meta for the post with an ID of 1. You can then loop through the $results array and do something with each result.

Now that you know how to create custom queries for post meta using wpdb, you can start using this powerful tool to retrieve the post meta data you need for your plugins and themes.