Mastering WordPress Meta Data: An Introduction To Meta Data

Part of what makes WordPress a powerful CMS tool is the ability to use the wp_postmeta table to store custom fields. These custom fields may contain additional information such as SEO fields added by an SEO plugin, or may be used to display additional front-end content.

By adding this additional information to fields you can create custom queries to organize the display of your content by.

Learning to work with this data is a great way to learn core PHP concepts including arrays, loops and objects. To become a master of WordPress meta data, you must master arrays of all sorts.

In this series of tutorials, I will show you various ways of working with WordPress post meta data and user meta data as concrete examples of how to work with arrays.

You will learn the difference between an indexed array and associative array as well as how to make sense of multi-dimensional arrays.

What Is Post Meta Data?

The most technical definition of post meta data is any information that is stored in the wp_postmeta table. By default that's not much, but anytime a plugin or a theme adds a custom field, the data entered into that field will be saved into the wp_postmeta table.

A good example would be the SEO fields that an SEO plugin adds. For example, think of a post meta description. That field is stored in the postmeta table.

Additionally, user profiles in WordPress can be extended with custom fields. In a relationship that mirrors wp_posts and wp_postmeta, these fields are stored in the wp_usermeta table - not the wp_user table.

In this series, we won't be discussing how to add custom fields in WordPress - that is it's own topic entirely. Custom fields can be added in the post editor with the "Custom Fields" meta box, by implementing them directly into a plugin or theme, or by using a specialized framework such as Pods or Advanced Custom Fields.

Technically speaking, post meta data is any information that is stored in the wp_postmeta table. As we've previously mentioned, a good example of post meta data is the meta description field that is introduced through plugins like WordPress SEO.

yoast

Methods for Getting Meta Data

Using get_post_meta()

WordPress gives us a variety of ways to get meta data for a post.

We can get just one field using get_post_meta() by specifying that field. For example, to get a field called 'foo' from the current post in the loop we could do get_post_meta( get_the_id(), 'foo', true );.

Notice that we set the last argument to true. This argument called "single" determines if we want a single value or an array of values back. If we wanted we could get all values for the post into one variable, or all values from field.

Using WP_Query

Sometimes we want to build an entire loop around a meta data, and this is where WP_Query is very useful. WP_Query allows us to do what is called a "meta query" where we query the posts based on one or more meta fields, and return the posts that have values that match a specific value. The result is a collection through which we can loop.

For example, imagine we have a field called "author_name" and we wanted to return every post where the field author_name had the value value 'J.R.R. Tolkien'. WP_Query allows us to do this easily - we'll be taking a look at this in-depth during the fourth part of this series.

Posts Aren't All!

Posts are not the only type of data that has meta data. For example, you know all of those fields that are available in the user profile? Those are all meta fields, instead of being stored in the wp_postmeta table, they are stored in the wp_usermeta table.

As a result, we have special functions and classes for user meta information. The functions get_user_meta() and get_author_meta() are the equivalent of get post meta for users. WP_Query also has its own users equivalent - WP_User_Query.

Objects Versus Arrays

When you use functions like get_post_meta() to return more than one field, you will likely get what is called a multi-dimensional array. A multi-dimensional array is also known as an array of arrays. Each array within the multi-dimensional array could be a multi-dimensional array.

Sound confusing? Don't worry! In this series, we are going to cover how to read multi-dimensional arrays in order to "drill-down" to the index you need.

Finally - as we mentioned earlier - another method for getting information about a post is to use WP_Query. When you use the WP_Query class you are using what is called object-oriented PHP and what you return is different than an array - it's an object.

In some ways objects are similar to arrays in that they may contain arrays of information. But with an object you can use any of the functions available on class. This means that not only can we use a WP_Query object to return a value for a meta field, but we can also use its internal methods, the most common of which is the_post().

Up Next...

At this point, we've taken a survey of the various types of meta data, how they are stored, how they can be represented, and how to retrieve them.

As we continue with this series, we'll take a much deeper look at each aspect of the meta data tables, the associated APIs, and how we can leverage them to introduce functionality and flexibility into our projects.

Tags:

Comments

Related Articles