Mastering WordPress Meta Data: Understanding and Using Arrays

In the first part of this series, we covered what WordPress meta data is, how it can be retrieved, and the various data types (such as objects or arrays) in which it can be returned. Now it's time to learn about the different types of arrays.

When you write an array manually you know what its structure is an what the name of each index is. But when you are building arrays by querying a database, you are going to need to do some detective work to determine the structure of the data returned and the names of the indexes.

Array Types

Before actually jumping into the various types of arrays that are available, it's important to understand that a PHP array is any variable that contains more than one piece of data.

Associative Versus Indexed Arrays

The simplest example of an array is a comma separated list of values, inside of the function array(), like this:

PHP automatically indexes arrays created in comma separated lists, assigning each item a numeric index, starting at zero. This means that we could retrieve the value of the second item 'Leia' of the $heroes array by specifying the index 1.

Here is what that would look like:

For a practical WordPress example, take a look at the function wp_get_attachment_image_src() which returns an indexed array of information about an image.

We can get the URL, width and height of an image using the numeric indexes from the array this function returns (note that the '7' being passed to the function refers to the attachment ID, and in this case it's arbitrary for the purposes of our example):

Of course, keeping track of the index numbers in arrays can get confusing, which is why PHP allows us to create associative arrays. Returning to our $heroes example, we could give each item in our array a name:

We call these names keys. In this array, we would say the key 'unlikely_hero' has the value 'Luke'. We can get the value of this key by placing it in brackets next to the variable. For example:

Multi-Dimensional Arrays

So far, we've looked at arrays that are essentially list of items, but arrays can also be comprised of other arrays. We call these multi-dimensional arrays. When creating a multi-dimensional array, we are essentially have a list of lists, where each index or key in our array is defined by another array.

For example:

This sort of array is very useful as it allows us to loop through it, writing our markup once and letting PHP iterate over it as many times as possible.

Tools for Detective Work

As I said at the beginning of this article, working with meta data can often involve some detective work to discover the array's structure. Like any detective, you need to ensure you have the right tools for the job.

Amateur developers modify their theme files to output values of arrays temporarily. But if you want to work quickly and efficiently you will do it the pro way--with debug console.

Amateur developers modify their theme files to output values of arrays temporarily. But if you want to work quickly and efficiently you will do it the pro way--with debug console.

What Is the Debug Console?

Debug Console is one of several add-ons for the Debug Bar plugin that no WordPress developer should be without. If you don't already have Debug Bar installed in your development environment you need to do so as soon as possible.

Once you have these two plugins installed you will be able to access a console from the admin bar, where you can run PHP code. Now instead of modifying a theme file, saving and refreshing you can simply experiment with code right in the browser.

Just click 'run' and see the results at the bottom of the screen. The best part is that any errors you generate will show in the results box, not on your sites front end. Fatal errors break the debug console, not your site.

I've only just scratched the surface of what Debug Bar can do, by the way. Also, both Debug Bar and Debug Console, as well as a ton of other cool utilities are included in the Developer Plugin, which I highly recommend. As you learn to work with meta data you may will also find the plugin Debug Bar Post Meta quite useful as well.

var_dump vs print_r

Now that you have a place to do your testing how do you explore the interiors of an array. For this work, PHP gives us two utility functions that we would never use for creating front-end output, but are perfect for diagnostic work—var_dump() and print_r().

Both of these functions take a variable containing an array as an input and output its contents for us to examine. var_dump() contains more information such as the data type (string, boolean, integer, etc.) and length of the data, while print_r() is designed to be more human readable and skips this additional information.

Using var_dump to Find an Index or Key

var-dump

Here is a screenshot of a var_dump of the meta data for a post. It shows how I drilled down to just one field. My goal was to get the value of the meta title field created by the WordPress SEO by Yoast plugin.

As you can see from the var_dump, that I generated from all meta fields for post ID #1 with get_post_meta( 1 ); I was working with an associative, multi-dimensional array, which contained an indexed array with only one index. By examining the var_dump, I found that the key I needed was '_yoast_wpseo_title' and that the actual information I needed was in the first index. To access that I put the key '_yoast_wpseo_title' in its own variable, and then echoed the first index like this:

This is a good example for illustrating the strategy to find the necessary key, but is not the most efficient way to get the information, once you know the key. So, after doing the detective work to find the right key, I would specify the key directly in the call to get_post_meta(). When we need just one key, you can specify it directly in the second argument of get_post_meta().

It's important to keep in mind that get_post_meta(1, '_yoast_wpseo_title' ); will not return the value we need, but instead return an array. By adding true for the third argument 'single' we can get just the first index, which is what we need.

So, to directly echo the SEO title, all in one line we would need to do this:

Using get_user_meta()

So far in this series, I've shown you how to work with the wp_postmeta table, which contains all of the custom fields added to a post type.

Users can have custom fields as well. User meta data, extra fields added to user profiles work much the same way as post meta data, but are stored in the wp_usermeta table. When working with user meta data, instead of get_post_meta(), we use get_user_meta().

These two functions work identically, they just get their data from two different tables in the database.

Here's an example using get_user_meta() to output a custom image as a link to an author's posts:

Up Next...

In the two parts of this series, we've examined the various types of meta data, how they are stored, and how we can retrieve them. Furthermore, we've looked at the two types of arrays that PHP offers, and how this corresponds to meta data associated with posts and users.

In the next part of the series, we'll take a look at exact how we can iterate through each type of data so that we have the power and flexibility to customize the output for our templates.

Tags:

Comments

Related Articles