Custom Fields are the way of enabling authors to store meta-data against an individual post / page in WordPress. The Codex describes some basic uses for this meta data such as displaying your current mood, weather, or listening habits, etc. Compared to other parts of the posting process, custom fields have received very little active development. I’ve been using them since WP 2.3 and can’t recall much aside from UI improvements that kept it consistent with the rest of the updates in 2.5 / 2.7. However, many people have begun to explore significant ways to enhance the CMS capabilities of WordPress through the functionality that custom fields do offer.
Learn to love get_post_meta
The get_post_meta function is a function you will come to know and love when working with custom fields in WordPress. It is used within the loop to present the value based on the name of the custom field.
<?php $key="rideshare_name"; echo get_post_meta($post->ID, $key, true); ?>
Beyond the basics
If you are doing more than just basic data storage / display with custom fields, the following links / examples will be of interest to you:
- Web Designer Notebook covers how to display multiple values for the same custom field name. This might be useful to display a list of ingredients for a recipe site or the parts needed for a set of DIY instructions.
- Tutorial9 has a great tutorial on the many different ways to use custom fields for storing images to represent a post. After uploading the photo, you store the filename of a full-size or thumbnail photo in a custom field and use get_post_meta to extract / display in the appropriate theme file locations.
- Cagintranet has 3 custom field tricks that operate per post: custom read more tags, thumbnails of related posts and post-specific css overrides.
- LiveXP shows how to get custom fields outside the loop. If creating a theme options page is beyond your skill, you can create a private page and store / retrieve the meta values from custom fields attached to that page wherever you need to.
- Two Words: More Fields. This plugin enables you to create custom write panels with custom field data types (radio buttons, drop-down, textbox, wysiwyg, etc). My most recent screencast gives a good overview of using More Fields to design a store location list using the plugin.
- Sidetrack Suggestion: I can easily see (and hope that) the type of functionality More Fields has explored gets built into the core as a part of the post-types being considered for 3.0. With the drag-drop interface of the widgets management area applied to the design of write panels, this would be a fantastic feature of a future version of WordPress.
- Justin Tadlock has written immensely on the power of custom fields. If you want a crash course in custom fields, his site is a great place to start.
- Smashing Magazine shows how to get posts with a specific custom field and specific value as one of their 10 useful WordPress Loop hacks. This is invaluable when capturing data that you want to filter for in query loops.
What actually inspired me to write this post was my recent efforts to build an elegant solution to a query quandry I was having related to custom fields – How to filter / query for multiple custom field key / values? The solution is one that could be of interest to theme / framework designers, plugin developers or possibly even the base concept for implementation into the core of advanced custom field functions WordPress.
If you have any other great custom field tutorials, please share them in the comments of this post.