Get Custom Field Values
- Author:Scott Reilly
- Version:2.5
- Last update:01 April 2008
- Compatibility:WP 2.1+, 2.2+, 2.3+, and 2.5
- Comments:go here
- Download:[ zip ]
- Description:
Easily retrieve and control the display of any custom field values/meta data for posts, inside or outside “the loop”.
-
Extended Description
The power of custom fields gives this plugin the potential to be dozens of plugins all rolled into one.
This is a simple plugin that allows you to harness the power of custom fields/meta data. You can define $before and/or $after text/HTML to bookend your results. If no matching custom field by the name defined in $field was found, nothing gets displayed (including no $before and $after) (unless $none is defined, in which case the $none text gets used as if it was the match). If multiple same-named custom fields for a post are defined, only the first will be retrieved unless $between is defined, in which case all are returned, with the $between text/HTML joining them). If $before_last is defined along with $between, then the text/HTML in $before_last is used prior to the last item in the list (i.e. if you want to add an “and” before the last item). Head down to the Tip & Examples section to see how this plugin can be cast in dozens of different ways.
You can filter the custom field values that the plugin would display. Add filters for ‘the_meta’ to filter custom field data (see the end of the code file for commented out samples you may wish to include). You can also add per-meta filters by hooking ‘the_meta_$sanitized_field’. $sanitized_field is a clean version of the value of $field where everything but alphanumeric and underscore characters have been removed. So to filter the value of the “Related Posts” custom field, you would need to add a filter for ‘the_meta_RelatedPosts’.
Installation
- Download the file get-custom.zip and unzip it into your wp-content/plugins/ directory.
- (optional) Add filters for ‘the_meta’ to filter custom field data (see the end of the file for commented out samples you may wish to include). And/or add per-meta filters by hooking ‘the_meta_$field’
- Activate the plugin through the ‘Plugins’ admin menu in WordPress
- Give post(s) a custom field with a value
- Use the function
c2c_get_custom()somewhere inside “the loop” and/or use the functionc2c_get_recent_custom()outside “the loop”; use ‘echo’ to display the contents of the custom field; or use it as an argument to another function
Tips & Examples
- With this simple invocation, you can echo the value of any metadata field (where, of course, ‘mymood’ is the custom field key of interest):
<?php echo c2c_get_custom('mymood'); ?> - Want the behavior of a moods plugins?
<?php echo c2c_get_custom('mood', '(Current mood : ', ')'); ?>Example results:
[if ‘mood’ is set to ‘happy!’] (Current mood: happy!)
(add filters if you wish to use smilies, etc) <?php echo c2c_get_recent_custom('mymood', 'Most recent mood: '); ?>- Want the behavior of a currently reading/currently listening to plugin?
<?php echo c2c_get_custom('reading', 'Currently reading :'); ?>[if value for ‘reading’ is ‘Cujo’] Currently reading: Cujo
<?php echo c2c_get_custom('reading', 'Currently reading: <i>', '</i>', '', '</i>, <i>'); ?>[if three ‘reading’ fields were defined, with values ‘Carrie’, ‘Cujo’, and ‘The Shining’]
Currently reading: Carrie, Cujo, The Shining<?php echo c2c_get_custom('listening', 'Now playing ', ' on my stereo.', 'nothing'); ?>[if value for ‘listening’ is ‘The Beatles’] Now playing The Beatles on my stereo.
[if no value defined for ‘listening’] Now playing nothing on my stereo. - Want the behavior of a Post Icon plugin (image to display for the post, not derived from category)?
<?php echo c2c_get_custom('post_icon', '<img alt="post icon" class="posticon" src="/wp-images/posticons/', '.gif" />', 'blank'); ?>Which would yield:
[if value for ‘post_icon’ is ‘wordpress’]
<img alt="post icon" class="posticon" src="/wp-images/posticons/wordpress.gif" /> - Want to do per-post customized ‘more’ text? (to override the default “Read the rest of this entry >>”) (This example will show that default, which you can of course change, if you don’t define the custom field ‘more’ for a post)
In index.php, look for this function:
<?php the_content(); ?>Replace it with this:
<?php the_content(c2c_get_custom('more', '<span class="more">', '</span>', 'Read the rest of this entry »')); ?> <?php echo c2c_get_custom('todays_link', '<a class="tlink" href="', '" >Today's Link</a>'); ?><?php echo c2c_get_custom('related_offsite_links', 'Here's a list of offsite links related to this post:<ol><li><a href="', '">Related</a></li></ol>', '', '">Related</a></li><li><a href="'); ?><?php echo c2c_get_custom('more_pictures', 'Pictures I've taken today:<br /><div class="more_pictures"><img alt="[photo]" src="', '" /></div>', '', '" /> : <img alt="[photo]" src="'); ?>
- With this simple invocation, you can echo the value of any metadata field (where, of course, ‘mymood’ is the custom field key of interest):
-
Template Tags
Functions
<?php function c2c_get_custom( $field, $before='', $after='', $none='', $between='', $before_last='' ) ?>
Template tag for use inside “the loop” and applies to the currently listed post.<?php function c2c_get_recent_custom( $field, $before='', $after='', $none='', $between=', ', $before_last='', $limit=1, $unique=false, $order='DESC', $include_pages=true, $show_pass_post=false ) ?>
Template tag for use outside “the loop” and applies for custom fields regardless of post.
Arguments
- $field
Required argument. The custom field key of interest. - $before
Optional argument. The text to display before all field value(s). - $after
Optional argument. The text to display after all field value(s). - $none
Optional argument. The text to display in place of the field value should no field value exists; if defined as ” and no field value exists, then nothing (including no $before and $after) gets displayed. - $between
Optional argument. The text to display between multiple occurrences of the custom field; if defined as ”, then only the first instance will be used. - $before_last
Optional argument. The text to display between the next-to-last and last items listed when multiple occurrences of the custom field; $between MUST be set to something other than ” for this to take effect.
Arguments that only apply to c2c_get_recent_custom():
- $limit
Optional argument. The limit to the number of custom fields to retrieve. - $unique
Optional argument. Boolean (’true’ or ‘false’) to indicate if each custom field value in the results should be unique. - $order
Optional argument. Indicates if the results should be sorted in chronological order (’ASC’) (the earliest custom field value listed first), or reverse chronological order (’DESC’) (the most recent custom field value listed first). - $include_pages
Optional argument. Boolean (’true’ or ‘false’) to indicate if pages should be included when retrieving recent custom values; default is ‘true’. - $show_pass_post
Optional argument. Boolean (’true’ or ‘false’) to indicate if password protected posts should be included when retrieving recent custom values; default is ‘false’.
-
Frequently Asked Questions
- Q: I added the template tag to my template and the post has the custom field I’m asking for but I don’t see anything about it on the page; what gives?
A: Did you echo the return value of the function, e.g.
<?php echo c2c_get_custom('mood', 'My mood: '); ?>
- Q: I added the template tag to my template and the post has the custom field I’m asking for but I don’t see anything about it on the page; what gives?
Release Log
- 01 Apr 2008 : v2.5 — Release Notes
- NOTE: Information and discussion for versions of this plugin prior to v2.5 are here.
- 26 Mar 2005 : v2.1 released –
- Removed the $filter argument from c2c_get_custom() and c2c_get_recent_custom()
- Replaced $filter argument with more robust filtering approach: filter every custom field via the action ‘the_meta’, filter specific custom fields via ‘the_meta_$field’
- Add argument $include_static (defaulted to true) to c2c_get_recent_custom(); static posts (i.e. “pages”) can be optionally excluded from consideration
- Verified to work for WP 1.5 (and should still work for WP 1.2)
- 08 Sep 2004 : v2.01 and v.202 release — minor bugfixes
- 07 Sep 2004 : v2.0 released :
- Added the new function
c2c_get_recent_custom()that allows retrieving custom/meta data from outside “the loop” - Better filtering (on meta field itself instead of final output string)
- Per-call filtering of meta fields
- Prepended all functions with “c2c_” to avoid potential function name collision with other plugins or future core functions… NOTE: If you are upgrading from an earlier version of the plugin, you’ll need to change your calls from
get_custom()toc2c_get_custom() - Changes to make the plugin WordPress v1.3 ready (as-yet unverified)
- Switched to MIT license
- Added the new function
- 22 Jul 2004 : v1.0 released — added argument of $before_last (which, went $between is also defined, will be used to join the next-to-last and last items in a list); added invocation of an action called ‘the_meta’ so that you can do add_filter(’the_meta’, ’some_function’) and get custom field values filtered as they are retrieved; to faciliate use of this plugin as the argument to another function, this plugin no longer echoes the value(s) it retrieves (user must prepend ‘echo’ to the call to get_cust())
- 30 Jun 2004: v0.91 bugfix release
- 30 Jun 2004: v0.9 released to the public
Copyright & Disclaimer
Copyright © 2004-2008 by Scott Reilly (aka coffee2code)Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Acknowledgements
Thanks to all those who have contributed feedback and support!
