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 TypeExamples
Standard fieldsTitle, content, excerpt, date, author, categories
Custom fieldsStar 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

  1. Open a post in the block editor
  2. Click the three-dot menu (⋮) in the top-right → Preferences
  3. Under Panels, enable "Custom Fields"
  4. The page will reload with a Custom Fields section at the bottom

Adding a Custom Field

  1. Scroll down to the Custom Fields section
  2. Click "Enter new" to create a new key
  3. Enter the name (key): star_rating
  4. Enter the value: 4
  5. Click "Add Custom Field"
  6. 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);
ParameterWhat It Does
$post_idThe post ID — use get_the_ID() inside the Loop
$keyThe custom field name (e.g., 'star_rating')
$singletrue = return a single value; false = return an array

Common Custom Field Use Cases

KeyValue ExampleUse
star_rating4Display star ratings on review posts
subtitleA deeper look at...Secondary heading below the title
external_urlhttps://example.comLink to external resource
price29.99Product price
is_featuredyesMark 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