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:
Get Custom Field Values
Scott Reilly
Last updated:
26 March 2005

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.


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.

  1. Download the file and unzip it into your wp-content/plugins/ directory.
    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

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.



252 replies on “Plugin: Get Custom Field Values”

I’m getting the collowing error:

Warning: array_map(): Argument #2 should be an array in /home/reelide/public_html/wordpress/wp-content/plugins/get-custom.php on line 95

Warning: implode(): Bad arguments. in /home/reelide/public_html/wordpress/wp-content/plugins/get-custom.php on line 99

Thanks for reporting the bug, Vincent! And thanks for suggesting a bugfix, guard. I had written a fix that did essentially what yours did, though slightly different, but I’m glad to be able to compare to yours to know that the fix should work. The plugin files have been updated to v0.91 as a result.

Hi Scott, and thanks for the very useful plugin, it’s an important part of a site I’m currently working on. I made some changes to the code, so it also allows Textile and includes a string before the last item in a list (like an “and”). Drop me an e-mail and I’ll send them your way.

Hi, Luís; small world. As it so happens the next release of this plugin will address the two situations for which you made code changes. Not sure about an ETA for the release, but it should be within the next few days. Are you converting your personal blog, or perhaps FM?

Plugin Updated to v1.0

A few new features and changes have been made to this plugin. Users of previous versions of this plugin should take note of the changes:

1. This plugin no longer automatically echoes the result. Instead, it returns the value it would’ve echoed. (See the examples above for the use of ‘echo’.) Why? So that this plugin could gain more versatility by being able to be used as an argument to other functions. For instance, you want customized “more” link text? Change this line in your index.php:

<?php the_content(); ?>


<?php the_content(get_custom('more', '<span class="more">', '</span>', '(more...)')); ?>

If you then define text for the ‘more’ custom field and use the <!–more–> feature of WP, your text will be used for the link. Otherwise, the default “(more…)” will be used. Also, you’ll be able to customize the appearance of the “more” link (thanks to the <span>).

2. Now you can filter your custom field values/meta data via the add_filter() function. The name to filter is ‘the_meta’. The end of the plugin file contains some commented-out examples of filters you could add.

3. An additional argument, $before_last, has been added. If defined ($between must also be defined), then the $before_last value will be used to go between the next-to-last and last items in a list. That way you can now get something like “, B, C, D, and E”

Hello Scott,

I have just installed your get custom plugin and I am hoping I can use it to do the following:

I would like to make a sticky of one post in each category within my site – i.e. one post that will remain at the top of the page in any given category.

Is this possible using your get custom plugin? Please, please tell me it is – and if so – how do I go about making it happen?

Hoping to hear from you soon:)

Thank you 🙂

Well, presently, it doesn’t seem easily achievable using the plugin. However, I am working on modifications to the plugin that *may* faciliate such a thing. Keep an eye out here or at the WP support forums for when I update this plugin.

There are a variety of ‘sticky’ post plugins already available that are specifically geared toward that task… (but I’ll still contemplate using custom fields for it).

Thanks Scott.

I have tried using the Adhesive plugin which appears to be the ideal option for what I am trying to do, but for some reason it just does’nt want to work on my setup.

I will keep an eye out for any new options you may find.

Thanks again 🙂

The plugin itself won’t add any data that isn’t already defined in the custom field values it obtains or that were specified in its arguments.

If you say what you’re trying to accomplish, I could direct you better, but the solution likely involves defining CSS-targetable elements when you send the arguments to get_custom().. Such as:

<?php echo get_custom('mood', '<b class="mood">(Current mood: ', ')</b>'); ?>

That way you could style this via b.mood or .mood.

If you just want the custom value part styled, you could do:

<?php echo get_custom('mood', (Current mood: <b class="mood">', '</b>)'); ?>

Hi, I download and installed this pluin just as i have installed others. But once I open any page in the browser, the entire coding of the get-custom.php shows up. What could have been wrong?

How exactly did you obtain the plugin file: via the zip file or by saving the .phps file into a .php file? I would need more information about the steps you have taken; I don’t know offhand what the issue might be.

Hi, I’ve tried your plugin in WP 1.3 (alpha 3) and WP 1.2, but I can’t get it to work:

Warning: Invalid argument supplied for foreach() in c:desarrolloappservwwwwordpresswp-contentpluginsget-custom.php on line 137

I’m using this piece of code:
<?php echo c2c_get_custom('publicado'); ?>

The plugin is activated and the custom field is exactly “publicado”
What I’m doing wrong?
Thanks in advance

Hi, thanks for yor quick answer. I’ve installed the new version on WP 1.3 alpha 3 and it shows the meta without problems, But if a post doesn’t use meta, I get this error:

Warning: array_slice(): The first argument should be an array in c:\desarrollo\appserv\www\wordpress\wp-content\plugins\get-custom.php on line 135

I use this code:

<?php echo c2c_get_custom('publicado', 'Publicado por: ', '2004', ''); ?>

I’ve included the third parameter (” for $none) in order to show nothing when no meta are set, but it doesn’t work.

Another question: is it possible to hide an existing meta, I mean, I want to hide some meta in certain posts (a sticky one, using Adhesive plugin, that creates a custom field called ‘sticky’). I’ve think about using CSS. is it right?

Thanks again for your great work

poper: Sorry about that. I was manually copying code changes from a test copy to the official copy and neglected two things that necessitated these two bugfix releases. v2.02 is available now, which should solve that problem. You don’t need to explicitly specify the $none since it defaults to not showing a meta if that particular one isn’t defined.

I’m not sure what you are asking about for your second question, about hiding an existing meta. Meta data is only shown if you explicitly make a call to show it (i.e. via c2c_get_custom()). Otherwise, the meta data isn’t shown. Unless you mean that you have defined a meta field that you want shown for all posts EXCEPT for certain posts (i.e. a sticky post), even if the sticky post has the meta field. Is that it?

It works like a charm! (both, in WP1.2 & WP1.3)
About the second question, forget about that, I was a bit confused, but your last comment has helped me a lot.
Thanks you!

Hi, nice plugin.

I do have a question though. c2c_get_recent_custom seems to work such that it will get $field from the latest post that has it defined, even if it is not the most recent post in wordpress. For something I’m trying to implement I want it to only get the $field from the most recent post if it is defined, otherwise use $none. Any idea about how I could hack your script (namely the query) to make this the case?

I’m currently working on running my fiction archive with WP and discovered your plugin. Thanks so much for making it! It’s perfect for adding values like ‘pairing’ or ‘fandom’ and worked like a charm – until today. When I went to add some new posts to I added a custom field, as usual – only today it simply doesn’t show up. It says ‘Custom field added’ but nothing changes, although it stills shows just fine on the older posts. Any idea why that could be?

Ruby: Thanks!

Naim: Good suggestion. I’ll see if I can figure out a good way to incorporate your request into a future version of the plugin.

Shirasade: Are you still having the problem? Offhand, I’m not sure what could be wrong, though admittedly I’m not fully clear on the nature of the problem. Do you assign the post an existing custom field key, or did you create a new one? Was there anything different about the custom field value text that you used? If you go back and edit the post, is the custom field still there (and therefore saved with the post and still exists).

Hi Scott,

I am trying to use your plugin to add custom keywords to a post when displayed in single mode. This would require that it works outside the loop, and that it only returns something when there is a custom field (ie “keyword”) for the post which is displayed.

How could I use c2c_get_recent_custom to do this?


[Sorry for this spam-ish comment: I am currently contacting a few WP plugin developers with this message… but couldn’t find a direct email to send you that… feel free to delete this comment afterward]

I just released version 1.0 of what should soon enough become the somewhat official WP Plugin repository (currently figuring out the domain name and how it will be presented from the main wordpress site).

The page is already flooded by users, but unfortunately still rather low on registered plugins (well, considering there are thousands of WP plugins floating around on the net).

So I have been making a round-up of everybody’s favorite plugins and I am currently working on getting these in, so as to get the DB off to a good start. Yours being one of them

You can have a look at the current DB here:

Ideally, it would be awesome if you could register it yourself, as it would both make life easier for me and give you greater control on how your plugin is presented (and easy access to admin options, for example if you want to notify users of an upgrade)… It is a really painless and quick process, probably less than 3 minutes (maybe 5, if you want to make it one-click installable):

However, if you do not feel like taking the time to do that now, I would actually like to go ahead and add it myself, so that users can have access to it in the meantime… Of course you’ll be free to regain control of its entry in the DB at any moment (simply would have to create an account and contact me to transfer its ownership). Before doing that, though, I first wanted to know if it was alright with you (and incidentally if couldn’t do it yourself 😉 …

In addition, I am currently working on v.2.0 whose top feature would be greater install options for developers (I’m really shooting for the 90% one-click instability)… I would love to hear from you what type of extra-install steps your plugin require (SQL, file perms etc)…

Sorry for the length and please do not hesitate to contact me directly if you got any question/suggestion/comment…


Is it possible to use this plugin as a ‘filter’ in order to display only entries that meet a certain criterion for the value of a custom field. If so, how would I go about doing this?

How would I use this to display a list of all the custom field data for every post? I’m using a custom field fLink for links to files and I want to make a page that lists all the links. Is this doable?

Holy Shmoly,

This would have to be one of the best keep secrets!.
Bloody handy plugin this one. I was looking for something to get my keywords inserted and I found this..WOW is about all I can say (and I’ve only used to call the meta so far and theres so much more) 🙂

Thanks a million for this one Scott

Hello there,

Found your plug in and love the possibilities of what it can do. ^^ I installed it on my plugin folder, activated the plug-in and the custom field comes out, but now it doesn’t seem to be showing up on my page. I’m not entirely sure what I’m lacking.

1. unzipped and uploaded the plugin (get_custom.php)
2. activated the plugin
3. added the code to the main index.php where I want it to show
4. made the custom field: set the key to “mood” and value to “happy” via editing my latest post, and then saving everything

Is there any step that I’m missing?

Talk about titilizing plugin!
It seems like the perfect plugin for the purpose of writing a unique , which is what I’m trying to do.
I created a “static” page (I’m running WP 1.5 alpha 6) called “about”.

I’ve added a custom field, via the admin tool, called “thispage”
with the value “about”. In the header template, I write

The plugin is activated in the admin tool, the custom value is added and visible even through successive edits of the page.

Please help!

ummmm… seems like the pertinent code excerpt got lost.

I was trying to write:

<body id="<?php echo c2c_get_custom('thispage'); ?>">

In order to get a unique body id.

But I only get a:

<body id="">

Uhm, bad ass plugin you got here. I couldn’t believe my eyes. I was going to get my PHP programmer to help us mod this up and bam! Already done.

I got one issue to ask though in the implementation of it.

On the home page of my blog, I want to keep a short and sweet summary of the posts, with a “read here for more” link underneath.

So under the proverbial:
<?php the_content(); ?>

I entered a few
<?php echo c2c_get_custom('Cool fea... etc.

The thing is, enter tool, truncates the body of the content, but doesn’t do the c2c’s stuff very well. It displays it in the homepage regardless, defeating the purpose that I’m trying to get.

I’ve only just started getting familiarizing myself with WP a month ago, so pardon in advance, if it’s something dumb and obvious.

Kind regards!

Rob: Offhand I don’t believe the plugin currently allows what you are looking for. c2c_get_recent_custom(), as you know, works outside the loop, but in so doing it isn’t associated with any particular post. It gets the recent occurances of custom fields. There are other plugins geared toward your request. The WordPress Wiki has two (at the moment) if you search that page for the term “keyword”.

Joey Horne: Depends on how you wish to retrieve posts via their custom fields. If you are looking to modify your main page to show and/or skip posts based on the presence/value of a custom field, that is one thing; if you are looking to pull a list of posts based on custom field value (i.e. for use in a sidebar or some such), that is another. The latter is a feature forthcoming in a later release of this plugin. The former is something that requires you to make some changes to index.php, for which I’d need more info from you to determine what you want done.

Joe: Using the plugin function c2c_get_recent_custom() you can retrieve all custom fields of a given name from all posts. Currently the fields are presented in reverse chronological order (an option allows you to change that so they are presented in chronological order) based on the date of the post that each custom field was associated with.

Rin: Did you echo the result of the function in index.php, like so:
<?php echo c2c_get_custom('mood'); ?>
Without the ‘echo’ nothing will appear on the page. (Earlier releases of this plugin did not require the ‘echo’…)

mquick: c2c_get_custom() only functions within “the loop”; the HTML <body> tag takes place prior to the loop. What you want is doable though. If you’re still interested, I can help you with it.

B.A.R.: I think I catch the gist of what you’re saying. You basically don’t want the custom field stuff to appear on the main page, correct? Assuming that is the case (and always the case), you could wrap the c2c_get_custom calls so they only appear when the post is viewed by itself. Something like this:

<?php if ($single) {
   echo c2c_get_custom('cool_features',... yadda ...);
   echo c2c_get_custom('rating', ...yadda...);


Thank you Scott.

I got another issue that has come up that’s a little perplexing.

When I got to edit an article that’s already been published, (say I forgot a custom field), it doesn’t work. I click on a Add Custom Field and it simply redraws the screen and wipes out what I wrote for it.

Anyone else experience this?

As it stands, I can only add custom fields to new posts that haven’t been published yet. Is this behaviour normal or did I mess up something unique to me?

Thanks again Scott for your answer. I’m going to give that a shot!


Using this plugin for a local install of WP-as-archive and quickly becoming its biggest fan. It seriously rocks.

Just a question, would it be possible to have two functions for the same custom field, but with two different ‘before’ and ‘after’ variables? I.e. –

<?php echo c2c_get_custom('some_field_key', 'before1', 'after1', ''); ?>
<?php echo c2c_get_custom('same_field_key', 'before2', 'after2', ''); ?>

I might need that for another plugin at some point. Thanks!