Categories
Package/plugin/module WordPress

Plugin: Get Custom Field Values

This plugin has been updated! Comments to this post are now closed. For the latest download, documentation, and link for posting new comments related to this plugin, visit the plugin’s new homepage at:
coffee2code.com/wp-plugins/get-custom-field-values
Name:
Get Custom Field Values
Author:
Scott Reilly
Version:
2.1
Last updated:
26 March 2005
Description:

Easily retrieve and control the display of any custom field values/meta data for posts, inside or outside “the loop”. The power of custom fields gives this plugin the potential to be dozens of plugins all rolled into one.

Notes:

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.

Installation:
  1. Download the file get-custom.zip and unzip it into your wp-content/plugins/ directory.
    -OR-
    Copy and paste the the code ( get-custom.phps ) into a file called get-custom.php, and put that file into your wp-content/plugins/ directory.
  2. 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) *NEW*: Add per-meta filters by hooking ‘the_meta_$field’
  3. Activate the plugin from your WordPress admin ‘Plugins’ page.
  4. Give a post a custom field with a value.
  5. Use the function c2c_get_custom somewhere inside “the loop” and/or use the function c2c_get_recent_custom outside “the loop”; use ‘echo’ to display the contents of the custom field; or use as an argument to another function
Functions:

function c2c_get_custom ($field, $before='', $after='', $none='', $between='', $before_last='')

  • $field : This is the name of the custom field you wish to display
  • $before : The text/HTML to display before all field value(s)
  • $after : The text/HTML to display after all field value(s)
  • $none : The text/HTML 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 : The text/HTML to display between multiple occurrences of the custom field; if defined as ”, then only the first instance will be used
  • $before_last : 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

function c2c_get_recent_custom ($field, $before='', $after='', $none='', $between='', $before_last=', ', $limit=1, $unique=false, $order='DESC', $include_static=true, $show_pass_post=false)

  • $field : This is the name of the custom field you wish to display
  • $before : The text/HTML to display before all field value(s)
  • $after : The text/HTML to display after all field value(s)
  • $none : The text/HTML 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 : The text/HTML to display between multiple occurrences of the custom field; if defined as ”, then only the first instance will be used
  • $before_last : 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
  • $limit : The number of recent custom field values you want returned; default is 1
  • $unique : Boolean value (either ‘true’ or ‘false’) to indicate if only the latest instance of $field should be considered, and subsequent occurances of $field would be skipped in the search; default is ‘false’
  • $order : The order the results should be returned, either ascending/increasing (‘ASC’) or descending/decreasing (‘DESC’); default is ‘DESC’
  • $include_static : Boolean (‘true’ or ‘false’) to indicate if static posts (i.e. “pages) should be included when retrieving recent custom values; default is ‘true’
  • $show_pass_post : Boolean value (either ‘true’ or ‘false’) to indicate if passworded posts should be considered in the search; default is ‘false’
Tips & Examples:

Do you simply want to retrieve the value of a custom field/meta data?

<?php echo c2c_get_custom('mood'); ?>


Want a moods plugin?

<?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)


Want 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 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'); ?>

[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 “(more…)”) (This example will show the default “(more…)”, 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>', '(more...)')); ?>

Related Links:
  • WordPress Support Forums announcement of Get Custom Field Values
Release Log:
  • 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() to c2c_get_custom()
    • Changes to make the plugin WordPress v1.3 ready (as-yet unverified)
    • Switched to MIT license
  • 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 (c) 2004-2005 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:
N/A

252 replies on “Plugin: Get Custom Field Values”

Great plugin! I use custom fields to tag articles I’ve written with date published, newspaper, and co-author. Would it be possible to get the custom field values to display on the parent page too?

The parent page lists all subpages, but it only shows their titles. I’d like to get date published and newspaper to appear as well (such as: ‘”The title of the article” in The Newspaper, 2005-01-01’).

Es posible?

Could you please answer the questions about WP2x compatibility? I’d like to try out the plugin, but I don’t want to introduce anything that won’t work – and it seems that several folks are questioning whether the plugin works for WP2x.

I realize that plugin authors work hard to provide cool functionality for free (most of the time)… so I don’t want to whine, but it would be nice if the version functionality was prominently displayed and kept up to date, even if you are too busy to answer all comments.

thanks!

OK, I’ve managed to get it working… but would also like to choose one of the custom fields as the orderby parameter in a query… is that possible?

something like:



but this doesn’t work, of course. It doesn’t break anything, but it also doesn’t work.

So many folks seem to want to do something like this… is there any way?

Help GREATLY appreciated!

[…] Often I am asked by newcomers to WordPress, “how do I attach a thumbnail to a post?”. WordPress doesn’t provide such functionality by default, but it does provide the means. In WordPress you can attach an infinite amount of meta-data to posts using the “Custom Fields” area – for example, to tell WordPress to use a certain thumbnail on a post. Get Custom by Scott Reilly allows you to easily retrieve any post’s meta data. […]

Hi there,

I think this goes in the same direction as jakob’s question. There are certain characters that will not display if put behind “value =” in the ini-file. Example “(“, “)”, or “!”. Actually, all options behind these characters will not be displayed from the plugin. It just quits reading the options and skips to the next field.
Is there a way to escape these characters in the ini-file, similar to the way you would escape charaters in php?

Other than that, awesome plug-in!

Cheers
Olli

Your plugin is a mainstay in my WordPress installations!

What needs to be done to allow me to insert custom field values into my RSS feeds? I can hack the wp-RSS2.php file, but the plugin doesn’t seem to be called when WordPress is loading it.

Thoughts?

[…] Mir waren gleich beim ersten Eintrag die “Benutzerdefinierten Felder” aufgefallen. Dort hat man die Möglichkeit beliebige Daten an einen Eintrag anzuhängen. Um die Daten später komfortabel verwenden zu können, benutze ich ein Plugin von coffee2code. Sofern ein mood/music/quote Daten vorhanden sind, wird am Ende eines Posts, bei den Autor/Kategorie Infos ein kleines Icon mit dem entsprechenden Inhalt gezeigt. Die Icons stammen aus dem genialen Silk IconPack von Mark James. […]

Hi, great plugin but is there a way to make this work on user comments?

For example allow them to place ‘Subject’ in their comments?

Many thanks for your help,

Great plugin, but I’ve run across one limitation. I’m trying to get an output with $before and $after if a certain field is present, and an output without $before and without $after if it isn’t. However, $none is returned wrapped in the other two values. Is there a way to return the value of $none on its own in this situation, without creating messy code?

Wonderful plugin – I’m trying to use it to define a different image for various sections of my website… is there a way to get the c2c_get_recent_custom(‘fieldhere’) to display the ‘fieldhere’ of the current page? It seems that currently it sets it for whatever the value was first set at…

Any ideas would be greatly appreciated!

Nathan

Scott, nice one. So far a very useful plugin.

I am having an issue with my pages which I use to define the heirarchy of my site. I have a title and subtitle to each page entry. I use these as textual display elements.

I have a number of pages each with their own unique text within the custom fields.

What I am finding is that is returning the same text for each and every page.

???

shouldnt it see the current page selected and grab the custom fields from that page?

cheers!
J

Hi, this plugin it’s incredible, but i have a question, how i can add custom fields to rss?
for example:
" alt="" /&gt;

u_u I not find help in WP forums sniff sniff…

thank you… FOR ALL!! ^^

How do I call the filed again within the function? I’m trying to build a dropdown… I can either call it in the option value or inbetween the options tags, but not both. Any ideas?

I’m using WP 2.0.4 and think there is a problem.

I am using this on Pages, not Posts, but don’t think that should matter.

I have 5 Pages, each with the custom field author. Each page has a different author.

I then put this in the page.php file and I get the last author for ALL pages. I was expecting to get the author for each individual page?

help please,
mark

[…] <meta name="description" content="<?php the_title(); ?> at <?php bloginfo(‘name’); ?> <?php the_excerpt(); ?>" /> <meta name="keywords" content="<?php the_title(); ?> at <?php bloginfo(‘name’); ?>. " /> That at least generates a meta-description, which to me is the more important of the 2 tags meta description and meta keywords – especially for avoiding Google’s duplicate content filters. However, there are meta-tag generators out there for WordPress. I’m currently playing around with the Xfish one, as I can control tags per page – quite important in a client install of a CMS – but there’s quite a few to choose from: Add Meta Tags – Adds the post?s categories and the post?s excerpt as keywords and description meta tags respectively, while viewing single posts. AutoMeta – Automatically generates and publishes Technorati Tags and Meta Keywords based on the content of your post (it utilises the MySQL full text index for word scoring). Once generated, tags can be hand-finished to ensure pertinence. Category Tagging – Provides a tag cloud and related posts. When using this plugin, you can use the WordPress’ categories as tags — no additional tagging plugin is needed. Creative Commons Configurator – Lets you set a CC license for your blog and set options to include licensing info in the blog header (RDF metadata), after the post’s body (HTML) and in the syndicated content (Atom, RSS2, RDF) through the CC RSS module. All this is done from a configuration page in the Administration Panel. Custom Field GUI – Enables the automated creation of form element user interface for entering Post’ custom field(s). FOAF Header – Add a FOAF link to your page’s header. FOAF Output – Personal profile page with friends (XFN) and interests, FOAF RDF/XML profile including trust statements, and extended RSS (1.0) output. Get Custom Field Values – Easily retrieve and control the display of any custom field values/meta data for posts. Head META Description – Automatically add a meta description tag to posts, Pages, categories and elsewhere on your blog. Fully customizable. HeadMeta – Provides per-post custom <link> and <meta> tags. Jerome?s Keywords – Associate keywords with your posts. Jerome?s Keywords: Related Posts – This Plugin uses Jerome?s Keywords and presents related posts according to the tags (keywords) of the current post. Jerome’s Keywords Manager – Manage keywords entered via Jerome’s Keywords. KeyCloud Widgets – Sidebar widgets to display your Jerome’s Keywords in various forms, including the venerable tag cloud. Keywords – Append keywords into Meta tags from ‘keywords’ custom field. META Relationship Links – Adds START, FIRST and LAST rel(ationship) link tags to your blog, as well as PREV(ious) and NEXT for individual posts. myStatus – Creates a subpanel of the Dashboard that allows you to create a number of fields to specify your current status and insert it into your templates. RSD 1.0 – Adds RSD support to WordPress. Tags in the Head – Tags in the Head attempts to make your website both more machine and human readable by using the ?Tags? you specify (from Jerome’s Keywords or Ultimate Tag Warrior) as META keywords in a page?s header. It also allows you to customize the meta description?s for various parts of your site. Tags in the Head combines good SEO with commonsense organisation using tags. Userextra – Extends user profiles to include admin-defined attributes, and provides for category access controls with per-user granularity. Usermeta – Adds API for WordPress 1.5 so that arbitrary meta data can be associated to users. Xfish_meta – Allows the inclusion of meta tags like keywords and description __________________ Search Engine Optimisation consultant Need search engine traffic? I can help: Britecorp SEO services […]

i feel really stupid, but after being through this page three times and doing google searches; I still cant get this showing.

plugin installed and activated as described, custom fields appearing correctly on the write/edit page, however the text isnt showing on the posts.

I have two fields (link + myspace) and am using the following ion the post body:

neither show, and I cant work out why….

Hi there,

I’ve been scouring the WordPress support boards, but I haven’t been able to find the answer to my problem, which is quite small (I hope).

My problem is that when I post an entry on my WordPress journal with more than one custom field, they all run together in a single paragraph without any line breaks. I only noticed this recently because I tend to only use one custom value (“Currently reading”). I tried to post two tonight and they showed up side by side in the same paragraph.

Is there a simple way to fix this?

Thanks very much in advance.

Ok, I have installed the plugin and activated it, but I can’t seem to get it to work. I’m not too familiar with php code, so the problem is my lack of understanding.

Do I need to add code into the loop inside of my index.php file? And then, what code do I need to place into my blog post and where?

Hi, I’ve added this to my site, but I want to do one more thing. I need the get_custom_field_values to show up in the content — OR — I need the get_custom_field_values to show up in the RSS feeds. I need this becuase I’ve made each instance of the get_custom_field_values essential to my content.

This is an awesome plugin, BTW.