Episode 26 of 32
Custom Fields
Add custom metadata to posts using WordPress custom fields — star ratings, extra descriptions, and custom data.
Custom Fields
Custom fields (also called post meta) let you attach extra data to posts and pages beyond the standard title, content, and categories. This is useful for things like star ratings, custom descriptions, featured labels, or any structured data.
What Are Custom Fields?
WordPress stores two types of data for each post:
| Data Type | Examples |
|---|---|
| Standard fields | Title, content, excerpt, date, author, categories |
| Custom fields | Star rating, review score, subtitle, external URL — anything you define |
Custom fields are stored as key-value pairs in the wp_postmeta database table.
Enabling Custom Fields in the Editor
- Open a post in the block editor
- Click the three-dot menu (⋮) in the top-right → Preferences
- Under Panels, enable "Custom Fields"
- The page will reload with a Custom Fields section at the bottom
Adding a Custom Field
- Scroll down to the Custom Fields section
- Click "Enter new" to create a new key
- Enter the name (key):
star_rating - Enter the value:
4 - Click "Add Custom Field"
- Update the post
Displaying Custom Fields in Templates
<!-- Inside the Loop -->
<?php
$rating = get_post_meta(get_the_ID(), 'star_rating', true);
if ($rating) : ?>
<div class="star-rating">
<span class="rating-label">Rating:</span>
<span class="stars">
<?php
for ($i = 1; $i <= 5; $i++) {
if ($i <= $rating) {
echo '★';
} else {
echo '☆';
}
}
?>
</span>
<span class="rating-value">(<?php echo $rating; ?>/5)</span>
</div>
<?php endif; ?>
get_post_meta() Explained
get_post_meta($post_id, $key, $single);
| Parameter | What It Does |
|---|---|
$post_id | The post ID — use get_the_ID() inside the Loop |
$key | The custom field name (e.g., 'star_rating') |
$single | true = return a single value; false = return an array |
Common Custom Field Use Cases
| Key | Value Example | Use |
|---|---|---|
star_rating | 4 | Display star ratings on review posts |
subtitle | A deeper look at... | Secondary heading below the title |
external_url | https://example.com | Link to external resource |
price | 29.99 | Product price |
is_featured | yes | Mark posts as featured |
Advanced: Using ACF (Advanced Custom Fields)
For real projects, the ACF plugin provides a much better interface:
- Visual field editor — create fields with dropdowns, checkboxes, file uploads
- Field groups — organize fields and control where they appear
- Repeater fields — create lists of structured data
- Simpler template code:
the_field('star_rating')
ACF is the industry standard for WordPress custom fields.
Key Takeaways
- Custom fields store extra key-value data on posts and pages
- Enable them in the block editor via Preferences → Panels
- Use
get_post_meta()to retrieve custom field values in templates - Common uses: ratings, labels, prices, external links, featured flags
- For production sites, use ACF (Advanced Custom Fields) plugin for a better UI