Using WordPress Custom Fields: Introduction

I decided a couple of weeks ago that I wanted to do an ongoing tutorial series with WordPress custom fields. At first, I thought I’d simply write a complete guide, but that idea seemed daunting. It seems far easier to break down the guide into individual posts in a series. Instead of writing a general guide, I will write detailed tutorials on specific and practical uses for custom fields.

I’ve been using custom fields for various tasks on this site for a while. A few places you can see some custom field uses on this site are:

  • On the Reading page, you can see all of my book reviews pulled into a nice organized list. Under each book's title is the author's name.
  • My Projects page, there is a list of several subpages. For each subpage, there is an image, title, and description. This list of subpages is made possible by custom fields.

These are fairly simple examples, and this tutorial series will cover more advanced uses later in the series. Mostly, I will cover what I think are practical uses of custom fields. Of course, suggestions are always welcome. So, if you want something specific, but can’t figure out how to do it, don’t hesitate to ask. This is a learning experience just as much for me as it is for you.

What are custom fields?

Custom fields are additional information that you can add to your posts and pages.

The good starting point is checking out WordPress’ Using Custom Fields. It’s a very basic tutorial, but will give you some idea of what custom fields are. The Undersigned has a good tutorial also. Neither of these tutorials cover a lot of uses, and I think that’s why many people don’t know what to do with custom fields — there are not enough detailed tutorials.

The truth is that you can do pretty much anything you want with custom fields. Your imagination is your limit.

Basic Usage:

The first thing you need to know is where to input custom fields. On “Write Page” or “Write Post” in the WordPress Dashboard, there is section at the bottom of the page that lets you input as many custom fields as you want. Here is a screenshot:

View of the WordPress custom field box in the admin panel

There are two different things you can input into a custom field — “Key” and “Value.” It doesn’t get much simpler than that.

What it is a Key and Value? The Key is the name of the custom field and the Value is simply the value you want to give to that name. For example, you might write book reviews and want to add some additional information like the book’s author or publisher, give it a link through your Amazon Affiliate membership, create a star rating system with images, or a multitude of other bits and pieces.

What do I do with this information? You can pull this information out in posts, on other pages, into sidebars, or wherever.

To give a list of your book reviews in alphabetical order with the author’s name, you could create a custom field with a Key named “author” and a Value of “The Author’s Name” for several posts, or reviews. After writing these reviews and putting them into your “Book Reviews” category, you would put together a page called “Book Reviews” or create a category-specific template for your “Book Reviews” category.

Here’s how you would list each book review (category=27 would need to be changed to your “Book Reviews” category ID):

<ul>
<?php
$book_reviews = get_posts('category=27&numberposts=-1&orderby=post_name&order=ASC');
foreach($book_reviews as $post) : setup_postdata($post);
?>
<li>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php $author = get_post_meta($post->ID, "author", $single = true);
	if($author !== '') {
	echo 'by ' . $author;
	} ?>
</li>
<?php endforeach; ?>
</ul>

This is pretty basic, but I hope you’re getting some kind of an idea what custom fields are and what you might can do with them. I don’t want to give too much away on the Book Review use yet because I will definitely write a full-blown tutorial on how to add cool information to reviews.

Now, that you’re warmed up a bit, stay tuned by subscribing to the feed or bookmarking this page for future reference. I will link all the articles in the custom field tutorial series for WordPress here.