What Is Custom Field in WordPress: Definition and Usage
custom field in WordPress is a way to add extra information to posts, pages, or custom post types beyond the default content. It stores key-value pairs that you can use to display or control additional data on your site.How It Works
Think of a custom field like a sticky note you attach to a post or page. This note holds extra details that WordPress does not include by default, such as an author's nickname, a product price, or a special date.
When you add a custom field, you create a key (the name of the note) and a value (the content of the note). WordPress saves this information with the post, so you can use it later in your theme or plugins to show or use that extra data.
This system lets you customize your site without changing the main content, making it flexible for many different uses.
Example
This example shows how to add and display a custom field called subtitle for a post in your theme's template file.
<?php // Get the value of the custom field 'subtitle' for the current post $subtitle = get_post_meta(get_the_ID(), 'subtitle', true); // Check if the subtitle exists and display it if ($subtitle) { echo '<h2 class="post-subtitle">' . esc_html($subtitle) . '</h2>'; } ?>
When to Use
Use custom fields when you want to add extra details to posts or pages that are not part of the standard WordPress content. For example:
- Adding a product price or SKU on an online store post.
- Storing event dates or locations for event posts.
- Showing author bios or special notes on articles.
- Controlling display options like featured colors or layouts per post.
They are especially useful when combined with custom themes or plugins that read these fields to change how content looks or behaves.
Key Points
- Custom fields store extra data as key-value pairs attached to posts or pages.
- They let you add personalized information without changing the main content.
- You can access custom fields in your theme or plugins using WordPress functions like
get_post_meta(). - They are useful for adding product info, event details, or custom display options.
- Custom fields make your site more flexible and dynamic.