One of the best ways to store and display additional information on your site is to use WordPress’s post meta through custom fields.
WordPress has the ability to allow post authors to assign custom fields to a post. This arbitrary extra information is known as metadata.
WordPress.org Codex
This metadata, otherwise known as “post meta”, is a relationship between a post and all the details surrounding a post. Any kind of data can be stored as a relationship to a post.
Let’s take a look at some use cases for other information you might want to relate with a post.
Custom Fields Example
A real estate agent who displays a listing of several houses on their website is a good example. For each of the houses, there are quite a few details involved:
- address
- price
- summary
- number of bedrooms
- number of bathrooms
- home owners association
Now, you could store all this information in the post content and leave it as a list. But there’s a better way.
WordPress has a section called custom fields to add each of these items in their own area. Then it can be retrieved on the front end for the user to see.
When editing a post, you can view the custom fields section by clicking on the three dots at the top right of the window, clicking “Preferences”, and checking the box for “Custom Fields” under the “Panels” section.
Once you’ve done that, you should see a section at the bottom of the editor for Custom Fields where each field has a Name and a Value. These fields allow us to add those pieces of information and store their relationship to the current post.
This is what the section might look like after we’re finished entering in all the information for our real estate listing example.
How To Display Post Meta In a Theme
Once the fields have been set up in the editor, the next step is to get those values displaying on the front end in the house listing.
WordPress includes a function to pull these values from the database as long as you know the key (Name of the field), and the post ID. In this case, we would get the post ID for the house we are posting the listing for.
Here’s what this may look like in your theme:
<ul class="post-meta">
<li>Price: <?php echo get_post_meta( get_the_ID(), 'price', true ); ?></li>
</ul>
And if we were to add in the rest of our custom fields:
<ul class="post-meta">
<li>price: <?php get_post_meta( get_the_ID(), 'price', true ); ?></li>
<li>address: <?php get_post_meta( get_the_ID(), 'address', true ); ?></li>
<li>bedrooms: <?php get_post_meta( get_the_ID(), 'bedrooms', true ); ?></li>
<li>bathrooms: <?php get_post_meta( get_the_ID(), 'bathrooms', true ); ?></li>
<li>homeowners_association: <?php get_post_meta( get_the_ID(), 'homeowners_association', true ); ?></li>
<li>summary: <?php get_post_meta( get_the_ID(), 'summary', true ); ?></li>
</ul>
It’s nice to have a system for adding this information, but we’re still pretty limited in the type of information to add and how that information is organized.
The last field, home_owners_association
is really a true/false value. Does this house have a home owners’ association? Unfortunately, with a true/false (or boolean) value, there are several ways to represent it:
- yes
- no
- true
- false
- 1
- 0
And if this same field is used on every home post, who knows how many combinations could be used. This can get unwieldy very quickly.
Fortunately, Advanced Custom Fields (ACF) was created by Elliot Condon (and recently acquired by Delicious Brains) to solve a lot of issues that this system for post meta introduces.
Soon I’ll write a post how we can utilize Advanced Custom Fields to make sure the data is the same type everywhere the field is used.
In Review
- WordPress can store metadata as post meta through the use of custom fields
- Custom fields can be used to store any additional kind of information related to the post (price, # of bathrooms, etc.)
- Since these custom fields can be replicated across the site, many posts can take advantage of displaying the same content in a repeatable way
- Although these fields are available, it can be easy to incorrectly use them if you forget what kind of type you used in other posts (making it difficult to have consistency across posts)