Hey there! As a developer with over 15 years of experience building WordPress sites, I‘ve seen firsthand the power of custom fields.
In this guide, I‘ll explore what you can do with custom fields in WordPress, along with code examples, tips from my experience, and expert insights from around the web. My goal is to help you solve problems and take your WordPress site to the next level.
Let‘s dive in!
Contents
What Are WordPress Custom Fields?
WordPress saves all your content in two parts:
-
The main post content you add in the editor.
-
The post metadata like title, date, author, etc.
Custom fields allow you to create your own metadata that gets saved alongside the default WordPress post data.
To access custom fields, you need to enable them first:
- On the post editor screen, click on the 3-dot menu.
- Go to Preferences > Panels.
- Check the box for Custom Fields and hit Enable & Reload.
This displays the Custom Fields meta box below the editor where you can add, edit, and delete custom fields.
Why Use Custom Fields?
Over the years, I‘ve seen companies use custom fields in really creative ways to enhance their sites. Here are some of the most common use cases:
Store Extra Data
Custom fields shine for storing data that doesn‘t fit into the standard WordPress fields. For example, an events site can use them to save venue details like room capacity, parking info, etc.
Dynamic Content
You can use custom field values to display different content across your site dynamically. For instance, showing unique call-to-action buttons for different posts.
Integrate Plugins
Many plugins leverage custom fields behind the scenes to expand WordPress in cool ways. SEO and social meta plugins often use them to store optimized title, description, and image data.
Enrich Posts
By adding extra metadata like recipes, charts, author bios, etc. you can provide readers with richer and more useful content.
Programmatic Categorization
You can automatically assign categories to posts based on custom field values like product type, movie genre, etc. to keep your site better organized.
Post Expiry
Setting an expiration date custom field allows you to auto-expire posts after a certain date, like for contests, events, etc.
Customize Design
Using custom field values, you can add custom styling, layouts, widgets, etc. for individual posts for endless design flexibility.
The possibilities are endless! Let‘s look at how to implement them…
Adding Custom Fields in WordPress
Adding a custom field is easy:
-
On the post editor, expand the Custom Fields meta box.
-
Enter a descriptive name for your field in the Name input.
Avoid vague names like "CF1". Using specific names like "venue_capacity" improves readability.
-
Add your desired value in the Value input.
For longer values, consider adding a UI with ACF instead of manual entry.
-
Click Add Custom Field.
Your custom field will now appear in the meta box and get saved with the post.
You can reuse existing custom fields by selecting them from the dropdown menu and entering a new value.
Tip: Plan your custom fields out as much as possible beforehand so you can maintain consistency across posts.
Displaying Custom Fields in WordPress
Adding custom fields won‘t automatically display them on your site. To do that, you‘ll need to modify your theme files.
The best way in my opinion is using a snippet plugin like WPCode that lets you add code safely without editing theme files directly.
To display a text custom field called mood
using WPCode:
- Install and activate WPCode.
- Go to WP Admin > Code Snippets > Add New.
- Enter this code snippet:
<?php
$mood = get_post_meta( $post->ID, ‘mood‘, true );
if ( $mood ) {
echo ‘<p>Mood today: ‘ . $mood . ‘</p>‘;
}
?>
- Set Code Type to PHP Snippet.
- Set Insertion to Auto Insert.
- Save the snippet.
This will output your custom field value inside a <p>
tag on all posts that have the mood
field populated.
The key is the get_post_meta()
function which returns custom field values for the current post.
To display a custom field outside the loop, replace $post->ID
with the specific post ID you want to target.
Tip: Always check for empty values before trying to echo custom field data to avoid errors.
Creating Custom Field UI with ACF
Manually adding custom fields isn‘t ideal for larger sites with multiple authors. A better solution is to create a custom user interface for entering field values.
The Advanced Custom Fields (ACF) plugin makes this really easy.
Once installed, ACF adds a new menu where you can create field groups with customized interfaces.
For example, to create a mood custom field group:
-
Go to Custom Fields > Add New Field Group.
-
Enter a name like "Mood".
-
Click Add Field and select Text Field.
-
Configure the field settings:
- Field Label: Today‘s Mood
- Field Name: mood
- Default Value: (leave blank)
-
Click Save Field Group.
Now you will see your custom fields UI on all posts and pages. The values get saved as custom fields automatically behind the scenes.
You can then display them using get_post_meta()
like we saw earlier. This method provides a smooth editing experience for content creators.
Tip: Take some time to plan your field groups and fields for maximum efficiency. Refer to existing posts and templates to identify fields you need.
Common Uses and Examples from My Experience
Now that you know the basics, let‘s explore some common real-world examples from my experience with custom fields.
Show Related Posts
To show related posts from a category saved in a custom field:
<?php
// Get related category from custom field
$related = get_post_meta( $post->ID, ‘related_posts‘, true );
// Create WP_Query to get posts from category
$args = array(
‘category__in‘ => array( $related )
);
$related_posts = new WP_Query( $args );
// Output posts in a list
if ( $related_posts->have_posts() ) {
echo ‘<h3>Related Posts</h3><ul>‘;
while ( $related_posts->have_posts() ) {
$related_posts->the_post();
echo ‘<li>‘ . get_the_title() . ‘</li>‘;
}
echo ‘</ul>‘;
}
// Reset query
wp_reset_postdata();
?>
This fetches the related category from a custom field and displays posts from that category in a clean list format.
Highlight Priority Content
To highlight high priority posts with custom styling:
// functions.php
function highlight_priority( $classes ) {
global $post;
// Check for priority custom field
if ( get_post_meta( $post->ID, ‘priority‘, true ) == ‘high‘ ) {
// If priority is high, add custom CSS class
$classes[] = ‘priority-high‘;
}
return $classes;
}
// Hook our function to post_class filter
add_filter( ‘post_class‘, ‘highlight_priority‘ );
Then add this CSS:
/* style.css */
.priority-high {
background: #ffe9bc;
padding: 20px;
border-radius: 8px;
}
This makes it easy to highlight high priority content for your readers by simply adding a custom field.
Auto-Expire Posts
To automatically expire posts after a certain date:
// functions.php
function post_is_expired( $content ) {
global $post;
// Get expiration date from custom field
$expiry_date = get_post_meta( $post->ID, ‘expiry_date‘, true );
// Check if current date is past expiry
if ( $expiry_date && date( ‘Y-m-d‘ ) >= $expiry_date ) {
// Modify content if expired
$content = ‘This post has expired‘;
}
return $content;
}
// Hook our function to the_content filter
add_filter( ‘the_content‘, ‘post_is_expired‘ );
This makes managing expiring content like contests a breeze!
Many More Options
Over the years, I‘ve used custom fields for things like:
- Storing author social profiles
- Adding custom styling for landing pages
- Displaying related products
- Gating content for members
- Adding podcast embeds
- Dynamically displaying videos
- And much more!
They open up so many possibilities for customizing WordPress.
Tips and Best Practices
Here are some tips I‘ve learned for using custom fields effectively:
-
Use consistent naming like
author_bio
,related_products
, etc to stay organized. -
Watch out for blanks – check for empty values before trying to display fields to avoid errors.
-
Consider adding a UI with ACF to improve the admin experience.
-
Test thoroughly – preview different posts to make sure your theme handles custom fields properly.
-
Target selectively – only display custom fields where relevant to optimize performance.
-
Validate carefully when connecting to external databases.
-
Use snippets safely – a plugin like WPCode lets you add code without touching theme files.
-
Plan diligently – keep a checklist of fields required for different content.
-
Learn best practices – study the WordPress codex for examples.
Following these tips will help avoid headaches and get the most out of custom fields!
In Conclusion
If you made it this far, hopefully you now have a solid grasp of custom fields in WordPress.
As you can see, they provide an incredibly flexible way to supercharge your site!
The key is planning your fields carefully, integrating them cleanly into your theme, and using them judiciously.
Let me know if you have any other questions. I‘m happy to help you take your WordPress site to the next level with custom fields!