BytePlate Brand Navigation
  • About
  • wpCultivate
  • Services
    • Brand Consultation
    • Identity & Print Design
    • WordPress Websites, Maintenance & Services
    • E-Mail Campaigns & Social Media Branding
    • Interactive Presentations & Messaging
    • Search Engine Optimization (SEO)
    • Hosting Services & Maintenance
    • WordPress for Lawyers, Attorneys & Firms
  • Projects
  • Blog Bytes
  • Contact Us
  • Search
  • About
  • wpCultivate
  • Services
    • Brand Consultation
    • Identity & Print Design
    • WordPress Websites, Maintenance & Services
    • E-Mail Campaigns & Social Media Branding
    • Interactive Presentations & Messaging
    • Search Engine Optimization (SEO)
    • Hosting Services & Maintenance
    • WordPress for Lawyers, Attorneys & Firms
  • Projects
  • Blog Bytes
  • Contact Us
  • Search
Home Blog Bytes Creating Custom Fields Manually in WordPress

Creating Custom Fields Manually in WordPress

Over the past 12 months, I’ve written a number of posts that focus on adding functionality to WordPress using custom fields. We’ve looked at creating custom post list templates, crafting the perfect travel blog and more.

While plugins like CustomPress and Advanced Custom Fields make creating custom post type easy-peasy, if you want to really understand how they work you need to take a look under the hood. So in this post, I’m going to show you how custom fields can be created manually.

Let’s get stuck in.

Exposing the CMS Aspect of WordPress

To me, custom field functionality is the basis of a CMS system. Custom posts and taxonomies are all great, but if you want to build something other then a blog-type system you need the ability to bind data to your posts.

The two primary ways to do this in WordPress are custom fields and custom meta boxes. Before we look into how these are used I think it is important to understand the underlying mechanism: post metadata.

What is Post Metadata?

Post metadata – or post meta – is a term that describes any sort of data that is attached to a post. Each single piece of data is stored in the wp_postmeta table, which has four columns: ID, post_id, meta_key and meta_value.

Post meta in the database.
Post meta in the database.

The screenshot above is from phpMyAdmin, which shows the raw database data. The two rows shown are both attached to post_id 3974. The first row was added by WordPress to indicate who edited the post last. The second value is used by an SEO plugin to save the SEO title.

WordPress uses post meta internally for a number of things. You already saw how the last editor was saved. Another prominent example is saving a post’s featured image. When post 3974 receives a featured image a new post meta row is created with the meta key of _thumbnail_id. The meta value contains the ID of the assigned image.

Custom Fields and Metaboxes

Both custom fields and meta boxes are user interface elements that allow you to input data into WordPress. The custom field section is provided by WordPress and hooks straight into the post meta functionality described above.

WordPress Custom Fields
WordPress Custom Fields

When you enter a name and a value you are directly creating rows in the postmeta table.

Metaboxes on the other hand are essentially UI helpers built into WordPress. They give you an easy way to add input mechanisms to post edit pages. You can choose to hook them up to the post meta functionality but you can use them for other things as well. We wrote about this recently in Creating Custom Post Meta Boxes in WordPress, so we’ll focus exclusively here on metadata.

Manipulating Metadata

A very user-friendly way to manipulate meta data is through the custom fields user interface in the admin. As developers, we need to use code to add data since our plugin or theme needs to be able to add/modify/remove this information.

Luckily, this is pretty simple. We only need three functions: get_post_meta(), add_post_meta() and update_post_meta().

Let’s begin by grabbing some data to use.

Getting Post Meta

The get_post_meta() function takes three parameters: the ID of the post, the key and whether we’re grabbing single or multiple values. The first two should be pretty clear but the third may be confusing.

Remember how a row of meta data contains a key and a value? There’s nothing to stop you from adding multiple rows with the same key. This may seem like bad practice at first but can actually be pretty useful.

Let’s say you’re creating a recipe blog and you want to store the ingredients as post meta. You could use ingredient_1, ingredient_2 and so on for meta keys but this quickly becomes tiresome.

What you should do instead is use ingredient in each case. This would result in something like this in the database:

Multiple meta items with the same key.
Multiple meta items with the same key.

If you would use true as the third parameter of the get_post_meta() function only one of these rows would be retrieved. If you use false all rows will be returned as an array. Useful!

.gist table { margin-bottom: 0; }

Adding Post Meta

To add post meta use the add_post_meta() with three required parameters and one optional. The first parameter is the post ID, the second is the meta key, the third is the meta value.

The final – optional – parameter asks you to specify if this meta is unique or not. If you use false (or omit the parameter) the metadata will be added, even if one already exists with the same key. if set to true the data will not be added if a key with the same name already exists.

.gist table { margin-bottom: 0; }

Updating Post Meta

Updating post meta is very similar to adding it. In fact, you can use the update_post_meta() function to add data as well. If it doesn’t exist it will be created, just as if had used add_post_meta().

The function takes three required and one optional parameter. The three required ones are post ID, meta key and meta value as usual. The fourth parameter defines how to handle situations where multiple values with the same meta key exist.

If you omit this parameter all rows with the same meta key will be updated with the new value. If you use the fourth parameter you can specify a previous value. This will only update rows that have a value that matches your specified one.

.gist table { margin-bottom: 0; }

Useful Tips

That’s all there is tom know about post meta! You can now save values and use them later on. Before you put all this knowledge to work let me finish up with four useful tips.

1. Underscored Meta Keys

I’m sure you noticed that in our very first screenshot from the database, the meta keys began with an underscore. This has special meaning in WordPress: it signifies metadata that should not be shown in the custom fields user interface.

Any metadata you add normally like we did with ingredients will actually show up. If you think the user shouldn’t be able to edit the data directly just prefix it with an underscore and it will be hidden from view.

2. Meta Fields Handle Arrays

Always try and use as few meta fields as possible. If your plugin provides 10 options don’t create a meta key for each. Use one meta key and save all your options as an array. You can pass arrays straight into the update_post_meta() and add_user_meta() functions, WordPress will handle the rest.

In case you’re interested: WordPress serializes the data and saves it to the database. This results in a specially formatted string that is unserialized when it is retrieved from the database, returning to its array form.

3. All Metadata is Retrieved All The Time

To minimize overhead WordPress retrieves all meta data for a post if any single piece of meta data is requested. This means that you don’t have to worry about having 30 get_post_meta() function calls on a page. Only one database request will be made, everything is cached after that.

4. Get All Metadata at Once

The get_post_meta() function can return all meta keys and values for the given post. Simply omit the second and third parameters, just pass the post ID and you’ll get a nice array of all data in the database for that post.

Wrapping Up

Custom fields and the metadata system makes WordPress the workhorse that it is today. Even better, this mechanism is extremely easy to use and can empower your plugins and themes to be so much more.

Practice using the functions in your work to get your head around the basics and you’ll be creating great applications from there in no time.

Do you use metadata in a unique way? Let us know in the comments below.

Related posts:

  1. Creating Custom Content in WordPress: Taxonomies and Fields What makes WordPress a powerful CMS is the ability to…
  2. Creating Custom Post Meta Boxes in WordPress Meta boxes are a useful feature in WordPress that allow…
  3. Creating Custom Database Tables for Your WordPress Plugins Using custom database tables for your WordPress plugins is one…

View original post at WPMU

custom fieldsmetadataWordpressWordPress DevelopmentWPMU
BytePlate Brand has over 20 years of experience making brands happy through Website Design, Search Engine Optimization (SEO), Social Media, Print Design and more. Let us know how we can help your business grow:

About Us Form

    We will not share your address with any third parties. Don't worry. You can trust us!
  • This field is for validation purposes and should be left unchanged.

Recent Posts

  • Scan, Sign, and Manage Your Documents Right From Your Phone
  • Scammers Can Deepfake Your CEO in Just 3 Minutes for $15 — Here's How to Stop Them
  • Most Entrepreneurs Are Getting YouTube Completely Wrong — Here's What Actually Works
  • What I Learned About Business Growth From 5 Successful Founders Who Started Small
  • You Made It to the Top — But No One Trusts You Yet. Do These 4 Things to Change That

Archives

  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • February 2025
  • January 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • July 2024
  • June 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • November 2020
  • October 2020
  • September 2020
  • August 2020
  • July 2020
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • December 2019
  • November 2019
  • October 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • May 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • April 2018
  • March 2018
  • February 2018
  • January 2018
  • December 2017
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • July 2017
  • June 2017
  • May 2017
  • April 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • March 2015
  • February 2015
  • January 2015

Categories

  • Advertising
  • Branding
  • Charity
  • Data-Driven Marketing
  • Design
  • Fundraising
  • hummingbird
  • managed hosting
  • Marketing
  • Multisite Plugins
  • Online
  • optimization
  • performance
  • premium content
  • Reviews
  • Social Media Marketing
  • speed
  • Uncategorized
  • upgrades
  • web hosting
  • Wordpress
  • wpCultivate
  • WPMU DEV

  • Home
  • About BytePlate
  • Services
  • Some of Our Projects
  • Blog Bytes
  • Contact Us
FacebookXLinkedInRSS

© Copyright BytePlate Brand LLC. All rights reserved.