Hacks, tips and tricks are some of the fastest ways to cut the learning curve of WordPress management. Learn WordPress Custom Field hacks, tips and tricks are probably one of the most useful resources for intermediate and advanced WordPress users.
Even if you’re still a beginner you should read this article because you will gain a great understanding of the power of WordPress.
Defining the Custom Field
A custom field is a meta-data array that will store information in each of your WordPress posts. It is a powerful method to create custom information and output in your posts. The meta-data info is a key/value pair where the key is a name of a field and the value is the information that will be displayed for the field. The key and value can then be easily output. Very neat!
You can create custom fields that display important information like what you’re eating for lunch (you thought that was just for Twitter) and you can also display useful things like review scores for a product.
To make a custom field create a new post or edit an existing one. In the top right corner of the admin dashboard you’ll find a “Screen Options” button. Click on that and make sure that “Custom Fields” is checked.

As you can see, mine wasn’t by default, so I had to check it to activate custom fields.
Now scroll down the post editor page until you find the Custom Fields box.

In the Custom Fields box, Name corresponds to Key and Value to Value. Depending on which theme you are using, in the dropdown under Name you may already see a bunch of options. Unless you know what you’re doing, don’t use those.
Instead create a new entry by clicking Enter New. Make sure you use a unique name.
For the purposes of this example, I’m going to make an custom field called “my_mood”, so that my readers will know what sort of mood I was in when I was writing this post. For the value I’m going to enter, “Happy”.

Once that is entered click on “Add Custom Field”. Now when you click on “Publish” or “Update” for the post, that new custom field data will be added to the database.
If I want to add my mood to future posts as well, I can now select “my_mood” from the drop down menu under “Name” and then just type in my mood.
Output Your WordPress Custom Fields
Most people output the custom fields at the end of a post by pasting this code inside the single.php file within the WordPress Loop:
This will output all of your custom field key/value pairs into an unordered list that you can style using CSS. If you have many custom fields, but you only want to display specific ones you should take a look at the WordPress Codex for some great examples.
On a side note, most plugins take advantage of custom fields for their functionality.
Conditional Statement Fields
If you have a custom field that only needs to be active every so often, the way to do it is to condition its activation via a parameter. Thus, when not in use, the field will be replaced by default styling/content:
ID, 'songs', true);
//Checking if anything exists for the key Songs
if ($songs) { ?>
Listening to:
Listening to: Nothing right now
The code states that, for a post, if you leave the custom field unchecked, it will just be ignored, so you won’t have additional fields in your post that are just empty.
Using A Custom Field As A Conditional Statement
On the other hand, you can also use a field as the statement to generate a use pattern, to “trigger” an event:
For instance, you can choose to display a post only if it has a specific custom field.
This is done via the query-posts function allowing you to display posts only if they contain a certain key value.
This can be useful as a search tool. To keep with the music example above, say you want to see posts that were creating when you were listening to a specific song, by entering query_posts(‘meta_key=music’); you can generate the posts that have the specific key (song).
You can also use the custom post to determine whether a button will be present or not. If you don’t want the Digg Button displayed all the time use the following code:
ID, 'Digg', $single = true);
// if there's a single page digg button
if($digg !== '') { ?>
What the code does is add a custom field for you. For each post you create you can now select if the Digg button will be displayed or not. The key is Digg and the value can be true or false
Another a great use of custom fields is to enter a guest poster’s name and info. Here’s how to do it:
add_filter( 'the_author', 'guest_author_name' );
add_filter( 'get_the_author_display_name', 'guest_author_name' );
function guest_author_name( $name ) {
global $post;
$author = get_post_meta( $post->ID, 'guest-author', true );
if ( $author )
$name = $author;
return $name;
}
The new field will contain a name – the guest author’s name, and the value, which can vary (the name of the author is entered here)
This can save you a lot of fiddling time, especially of you use a lot of different authors and don’t want to create custom author pages for all of them, but still want their names to be displayed properly.
Custom Headers, Sidebars And Footers Via Custom Fields
It’s alright to use the same headers, sidebars and footers for most pages, but you might want to change it up for some pages. You can do this with a a custom field.
To do it you’ll need to:
//make sure this is your single.php or page.php
And then add the following below it:
post->ID; $sidebar = get_post_meta($postid, "sidebar", true); get_sidebar($sidebar); wp_reset_query(); ?>
Say, if you have a magazine type website, you can select from different styles of pages in an instant, which is a great time saver.
Creating And Setting An Expiration Date For A Post
Maybe you’re posting something that you want “flushed” out of your website after a while. To do it you need to add the following code:
$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}
$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
// For example…
the_title();
the_excerpt();
}
endwhile;
endif;
?>
Then, you set the amount of time for the post in the expiration custom field and when it counts down to zero the post will no longer be available. Neat and simple!
Individual Post Styling Via Custom Fields
You’ll need to create a custom field – post class – and then add a value, which will actually be your class (a styling). For example, if you want a post to have an “anniversary” look, you need to add the following code in the loop:
ID, 'post_class'); ?> Replace the div class with
Optionally, you can edit your header.php file with the code:
ID, 'customstyle', true);
if (!empty($customstyle)) { ?>
(Make sure the code is between head tags)
Then you can create a custom style by entering the “customstyle” css text as a value, for instance:
#imageborder{border: 5px solid #ccc;}
As you learn more about the custom fields, you will think even more use cases. They allow you to extend your WordPress site as much as you like without the use plugins and the headache they can sometimes bring.