To display a selected custom field in a WordPress theme, retrieve it with get_post_meta() in the template for the view you want to change, then render it with markup and escaping appropriate to its value. For example, a single-post template can show a plain-text subtitle like this:
<?php
$subtitle = get_post_meta( get_the_ID(), 'subtitle', true );
if ( '' !== $subtitle ) {
echo '<p class="post-subtitle">' . esc_html( $subtitle ) . '</p>';
}
?>
This example assumes subtitle contains plain text. Use suitable markup and escaping for the actual field type.
Choose the template that renders the view
Place the code where WordPress renders the post or posts whose metadata you want to show. For a single post, use the template handling that individual post; to display a field beside multiple posts, use the relevant loop or list template. Template files determine how content is displayed, while template tags retrieve database content for use in theme files. See the WordPress guides to template files, post template files, and template tags.
Retrieve the field with get_post_meta()
Pass the post ID, the exact metadata key, and true as the third argument when you want one value:
#1 Best Overall
$value = get_post_meta( get_the_ID(), 'field_key', true );
get_post_meta() returns a single value when its $single argument is true and an array of values when it is false. For a valid post with no matching field, the single-value form returns an empty string and the multi-value form an empty array. Scalar, non-serialized values are returned as strings, so convert or validate them if later code needs a number. The function reference documents the arguments and return behavior: get_post_meta().
Render optional values only when present
Check whether an optional field is empty before outputting its surrounding markup. Otherwise, a post without the value can show an empty label, blank element, or unwanted spacing. The example at the start uses an empty-string check for a plain-text value.
Rank #2
Match escaping to the output context
For plain text placed in an HTML text node, use esc_html(). A URL, an HTML attribute, or another output context needs handling appropriate to that context; do not assume that text escaping is suitable for every kind of value. The field’s intended type should determine both its markup and escaping.
Why get_post_meta() is preferable to the_meta()
the_meta() outputs an unordered list of eligible custom fields rather than letting the theme choose a specific field and its markup. WordPress marks it deprecated since version 6.0.2 and recommends retrieving values with get_post_meta() for manual rendering. For new theme code, the manual approach gives you control over which values appear and how they fit the design. See the the_meta() reference.
Rank #3
| Approach | What it displays | Status and control |
|---|---|---|
the_meta() |
An unordered list of eligible custom fields; protected keys are omitted. | Deprecated since WordPress 6.0.2; limited control over selecting and formatting individual fields. |
get_post_meta() with manual rendering |
The value or values for the key you request. | Recommended replacement for new code; lets the theme control selection, markup, and context-appropriate escaping. |
Understand underscore-prefixed metadata keys
Keys beginning with an underscore are treated as protected: WordPress omits them from the_meta() and does not show them in the built-in Custom Fields editing screen. That visibility behavior does not prevent theme code from retrieving a value when it knows the key and deliberately calls get_post_meta(). The WordPress documentation covers managing post metadata and rendering post metadata.
Quick Recap
Best Value
Rank #4
- Used Book in Good Condition
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.

