We need a solid definition of what a custom field is. But we also need to know what it is, how we can use it, and why.
Per WordPress codex, a custom field is:
WordPress has the ability to allow post authors to assign custom fields to a post. This arbitrary extra information is known as meta-data.
It goes on to say that they are key/value pairs. That’s important as we use the key to get access to the value.
Per WP Beginner, a custom field is:
Custom fields, also referred to as post meta, is a feature in WordPress which allows users to add additional information when writing a post. WordPress stores this information as meta data. Users can display this meta data by using template tags in their WordPress themes.
Using both of these definitions, what can you extract about a custom field?
- Additional information
- Stores information as metadata
- Key/value pair
What is the difference between Custom Field and Metadata?
Context. The difference is how you are relating the information to either the interface and user verses the code and data.
- Custom field – refers to the interface and user. It’s where you view, input, delete, or change a value in a form on an interface. It’s a field in reference to a form, i.e. form field. It’s “custom” because it does not exist in WordPress core. Why? You added it.
- Metadata – refers to the code and database. It’s under the hood and code-centric.
Custom fields, meta, and metadata are the same thing. It’s the key/value pair information that relates to the post.
Why Do You Want to Have Custom Fields?
Let’s use real world, practical examples.
- On a realtor website, there could be custom pieces of information to help viewers find a home, such as:
- Number of bedrooms
- Number of bathrooms
- Does it have a basement?
- Does it have a garage?
This type of information could be written inside of the WordPress post editor as part of the description, article, or content. But how would you be able to search or display homes that have 2 bedrooms and 1.5 baths? It would be difficult to do that when embedded inside of the content.
Instead, we build a custom field to store each piece of information separately. Now you can use that information. You can filter, display, use it to build custom HTML, and more.
Practical Real World Example
Here on Know the Code, I use custom fields for the episode configuration.
The picture above shows you a custom meta box I built for the episode configuration metadata. Here is how I use that information:
- Member access level – puts the badge in the playlist and listing on landing pages. Plus, it sets the access to the video.
- Episode Number – I use that in the playlist.
- Video ID – I use to get it to embed the Vimeo video.
- Runtime – It’s displayed in the playlist, on the episode, and summed with the other episodes to get a total lab runtime.
Break. Go rest your noodle for a couple of minutes.
Total Lab Runtime: 02:03:31
- 1 Lab Introductionfree 09:52
- 2 What is a Custom Field?free 15:55
- 3 What is a Meta Box?free 06:24
- 4 Project: Add a Subtitlepro 07:16
- 5 The Databasepro 11:48
- 6 Get Subtitle Post Metadatapro 15:26
- 7 Add Subtitle to Genesis Themepro 12:19
- 8 Update an Existing Custom Fieldpro 17:15
- 9 Add a New Custom Fieldpro 07:19
- 10 Delete a Custom Fieldpro 10:13
- 11 More Custom Field Functionspro 06:25
- 12 Wrap it Upfree 03:19