Understanding Custom Fields And Why You Should Use Them?

Quite often WordPress does and excellent job in fulfilling one’s website requirements. Find the right theme and you can make just about any kind of website. However, there are times when your chosen theme does 9 out of 10 things but then there is one specific functionality that it lacks. For instance, you wish to add a specific data to all your custom post type but you can’t really go beyond the basics such as content, author, category and the likes. What do you do then?

Well, this is where WordPress custom fields come into the foray. Also known as post metadata or even post_meta, WordPress Custom Fields basically store additional information on a per-post basis. Naturally, by know you would have realized that those tiny bits of information that are output to the screen for every post such as author, category, date of publication, last modified are all post metadata. By using custom fields you can add to these metadata.

What truly sets custom fields apart is that they exist on a post level but are essentially a key-value pairing and thus can easily be retrieved from the database with a few wordpress functions.

First Part – Adding The Custom Fields

While there are several plugins that make this process easy, fact is you need not use any plugin. The custom fields interface in WordPress is pretty standard but really useful too. Simply unhide it using the screen options on your edit/add new post page and you can begin adding custom fields. All you need is to input a new key and a value. When your post is saved, this new postmeta too is saved to the database.

Retrieving Custom Field Data

This part is really easy. You can retrieve post_meta by using the WordPress function called get_post_meta(). The first parameter for this function is required and everything else is optional. All you have to pass as parameter to get this function to work is the post ID you want to retrieve post_meta for.

The other option is to use the get_post_custom function that is much simpler and requires less typing too. However, it is less powerful than the get_post_meta() function.

To use the above retrieval functions within the WordPress loop you simply need to use  get_post_meta(get_post_ID());

Improving Custom Fields Interface

Custom fields inherent in WordPress is a boon because it is so simple to input key-value pairs but that also happens to be its bane in many instances. You can augment the existing custom fields using plugins that give you a lot more control and functionality. You could try Advanced Custom Fields or Pods plugins, which are my two personal favorites.

Why Use Custom Fields?

Frankly speaking when you use custom fields you can code much deeper in WordPress, automate certain retrieval tasks and make changes with ease. Imagine you have a star rating system and if you used custom fields and then used the php loop to output that then to change the location you simply make a single edit to your custom post file instead of changing the star rating on every post. Likewise, if you wish to extrapolate specific post data to a single page, because you are doing a comparison page or ranking page then with custom fields, you need not write everything.

For a more advanced tutorial on custom fields check out this amazing Tuts+ tutorial by Joe Clifton.

Leave a Reply

Your email address will not be published. Required fields are marked *