WordPress Custom Fields

What is a custom field?

A custom field is simply a way to add the same type of information to some or all of your WP posts. Just as all posts can currently have a title, category and tags, as well as content, you might choose to add custom fields to show your mood, the music you're listening to, the deal of the week on your ecommerce website or the filename of a thumbnail picture you want to display with your post.

Many plugins also utilise custom fields: for example, if you're running the Sociable plugin to allow your readers to share your posts via social media, you'll see "sociableoff" as an custom fields option - unsurprisingly, it allows you to switch off Sociable for individual posts.

How to set custom fields

Let's take a simple example: I want to add my mood to each post. The Custom Fields section is towards the bottom of the Add New Post page.

To add a new custom field, put the field key, e.g. "mood", in the "name" box, and the value, e.g. "cheerful", in the "value" box. Click the "add custom field" to save.

Adding a new custom field

Adding a new custom field


If you've previously set a custom field, the name will appear in a drop-down list in the Custom Fields section. So for your next post, "moods" will already be available, and you can just add your new mood to the value box.

Using an existing custom field in a new post

Using an existing custom field in a new post



How to show custom fields in your posts

Here's the simplest way to get a custom field to show in your posts. Just add this code to your theme where you want the custom field information to show:

<?php $my_custom_field = get_post_meta($post->ID, "mood", true);
echo "<p>Mood: ".$my_custom_field."</p>"; ?>

"mood" in the first line within the double quotes should be the name of the custom field you want to show (but make sure you preserve the quotes).

Find out more about using custom fields from the WordPress Codex.


This question came via the Skribit widget in my sidebar: if there's a question you'd like answered, please post it!

Share this post:
  • email
  • del.icio.us
  • Facebook
  • FriendFeed
  • LinkedIn
  • StumbleUpon
  • Tumblr
  • Twitter

Tags: , ,

Posted by Sue on March 3, 2009 in NPG2WP, WordPress.

20 comments to "WordPress Custom Fields"

  1. Robert wrote:

    Which file in the theme? Thanks.

  2. Sue (@blogmum) wrote:

    Hi Robert,
    Thanks for the comment. It depends on where you want the information to show up (and also on how your theme's put together). If it's for the single post page, then normally that's single.php; if for the home page, probably index.php; if for search results pages, search.php etc. etc.
    Let me know if that doesn't answer your question.

  3. Angie wrote:

    Is there any way to make custom field values, clickable? Tags and categories are clickable, if you tagged your post with FOOTBALL, by clicking on FOOTBALL, it would bring up all posts associated with that.
    Can the same be done with custom fields?

  4. Sue (@blogmum) wrote:

    Hi Angie,

    Thanks for that great question :-D

    The short answer is yes, this can be done: you'll need to use a custom query though. More details on the Codex -
    http://codex.wordpress.org/Displaying_Posts_Using_a_Custom_Select_Query

    If you know some PHP, it's pretty straightforward. If you *don't* know any PHP and would like some help fixing this for your site, give me a shout.

  5. Emi wrote:

    Thank you!
    Is it possible to put in an if statement to only display if the custom field exists? I suspect it's possible, but don't know enough PHP to do so... Could you help me out?

  6. Cindi wrote:

    Clean, concise explanation. Thank you.

    I want to create PayPal buttons using custom field information. Do you think this is possible? Every item gets one post. I include data such as price, color, product ID, etc. I modify the page that displays the post. All my stuff would look neat and tidy because most of the placement is handled by CSS. At least that is my theory.

  7. andrew wrote:

    Hi Sue,

    My wife is a photographer and I use a custom theme I purchases because I'm not great with CSS or PHP. I know precious little html and I know programming and coding basics - but thats about it.

    The default wordpress theme (used to be called Kubrick I think) displays post categories at the end of each post, and the categories and other meta information are clickable links. The theme I'm using (Photocrati Emulsion) removes this detail and I've concluded so far that Custom Fields is how I'll probably need to accomplish this. The goal by doing this is enhanced SEO.

    Am I on the right track? If so, is there an easy way to do this using custom fields in each post? Do I have to modify PHP files at all? There is no 'single.php' file with this theme. I poked around the couple of php files that do exist and there is nothing within them that alludes to what I'm after.

    Thanks in advance for any help or direction!

    All the best,
    Andrew

    • Sue wrote:

      Hi Andrew,
      You're not really on the right track, sorry. Categories are just a template tag - the_category() and tags are the_tag() - you don't need custom fields for either. You'll need to modify a theme file to get them to show up, yes - I'm not familiar with that particular theme, but if there's no single.php, then (assuming that there are single posts somewhere) the file you'll need to edit might be index.php. Back-up, then have a play; if it all goes horribly wrong, you can put your back-up back.

  8. Just to let you know, I've been trying to figure out how to do this for a few days now. I've run across about fifty different tutorials on the web, and copied and pasted every bit of code I could find, tweeking it to suit my custom fields, and yours is the first one that's worked. So. You've just become my "go to gal" for coding :)

  9. golden tarot wrote:

    That was a fascinating read|. Your insights were very educational and made me reconsider the latest developments in these areas. If only more writers are as mindful and as passionate about telling the population relating to these issues as you, we aspiring journalists would not get such a bad rep. Appreciate expressing your self so articulately. You made my day.

  10. Adela Grammer wrote:

    Hey! Just thought I'd chime in. I really was impressed by this article. Keep up the awesome work.

  11. Riley Carter wrote:

    Football is a bit tiring game but it has given me great excitement and it is a good physical exercise too.';.

  12. Jackson Hill wrote:

    avid Football fan in here, Manchester United is my favorite team..`~

  13. That's very interesting read. I will return to your blog very soon.

Leave a Reply