Creating Custom WordPress Tables Using Metadata API

Creating Custom WordPress Tables Using Metadata API

WordPress Metadata API primarily deals with storing and retrieving data from metatables. By default, WordPress comes bundled with four meta tables,

  1. postmeta
  2. commentmeta
  3. usermeta
  4. termmeta

For each meta table, there are four functions which are used to add, update, get and delete the data from these metatables.

Although WordPress only has four tables out of the box, you are not restricted to use only these four tables. WordPress Metadata API is pretty easy to extend, allowing you to create more meta tables based on your requirements.

In this tutorial, we are going to create custom meta tables in WordPress using the Metadata API.

We’ll divide this whole process of creating custom meta tables in a series of steps which you can follow.

  1. Creating the table.
  2. Register the table with WordPress
  3. Define functions to interact with the table

Creating the table

Default metadata tables have four columns meta_id, object_id, meta_key and meta_value. We’ll use the same table structure for our custom meta table.

CREATE TABLE `wp_bookmeta` (
  `meta_id` bigint(20) NOT NULL AUTO_INCREMENT,
  `book_id` bigint(20) NOT NULL DEFAULT '0',
  `meta_key` varchar(255) DEFAULT NULL,
  `meta_value` longtext,
  PRIMARY KEY (`meta_id`),
  KEY `book_id` (`book_id`),
  KEY `meta_key` (`meta_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

You can use the above MySQL query to create the table. For this example, I am assuming that you are using the wp_ prefix for your database.

The structure of the table is pretty straightforward.

  • meta_id – Primary key for the newly created table.
  • book_id – id of the object the metadata row belongs to.
  • meta_key – The key used to identify the type of meta_value row holds.
  • meta_value – The actual metadata value to be saved.

Register the table with WordPress

In order to use the table with the Metadata API, we need to register it with WordPress. You can following code to do the same.

function hco_register_booksmeta_table() {
	global $wpdb;
	$wpdb->bookmeta = $wpdb->prefix . 'bookmeta';
}
add_action( 'init', 'hco_register_booksmeta_table' );

Define functions to interact with the table

Now in this last step, we’ll create four functions to interact with our database. These functions will act as a wrapper to Metadata API.

To add data to the table we will create add_book_meta function.

function add_book_meta($book_id, $meta_key, $meta_value, $unique) {
	return add_metadata('book', $book_id, $meta_key, $meta_value, $unique);
}

To update the data stored in the bookmeta table we’ll create update_book_meta function.

function update_book_meta($book_id, $meta_key, $meta_value, $unique) {
	return update_metadata('book', $book_id, $meta_key, $meta_value, $prev_value);
}

To fetch the data from the newly created metadata table we are going to create a get_book_meta function

function get_book_meta($book_id, $key = '', $single = false) {
	return get_metadata('book', $book_id, $key, $single);
}

Let’s create a function to delete the metadata from this table

function delete_book_meta($book_id, $meta_key, $meta_value = '') {
	return delete_metadata( 'book', $book_id, $meta_key, $meta_value );
}

And we are done you can use the above functions to easily manage the data for our database table. I hope you’ll find this tutorial helpful. In case you have any queries or feedback feel free to drop a comment.

Leave a Reply

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