Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use get_post_meta() when you want to hide or show part of the current post, and use a meta_query when posts without the field must be excluded from a list. These are different operations: a template condition suppresses markup, while a query condition removes posts from the results.

Choose the behavior you need

Requirement Use Result
Show a section only on posts whose field is populated or has a required value get_post_meta() in the template The post remains available, but the selected markup is not rendered when the condition fails.
Show only posts whose metadata key exists in an archive, related-posts list, or custom loop WP_Query with meta_query Posts that fail the metadata condition are not returned by that query.

Before writing the condition, identify the exact metadata key and decide whether “has the field” means that the key exists, its value is non-empty, or its value equals a particular choice such as yes. Those rules are not interchangeable.

Show content on the current post when the field is populated

Place the check in the template while WordPress is in the current post’s Loop context. Replace your_field_key with the key stored in your site’s post metadata.

<?php
$field_value = get_post_meta( get_the_ID(), 'your_field_key', true );

if ( $field_value !== '' ) :
?>
    <div class="custom-field-section">
        <!-- Markup shown only when the returned value is not an empty string. -->
    </div>
<?php endif; ?>

get_post_meta( $post_id, $key, true ) retrieves a single value for the specified post ID and key. The official reference documents important return-value behavior, so a non-empty-string test should be used only when that is your editorial rule: get_post_meta() reference.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Require an exact stored value

If the field is a flag or controlled choice, compare the representation you actually store instead of relying on PHP truthiness. For a field that stores the string yes:

<?php
if ( get_post_meta( get_the_ID(), 'your_field_key', true ) === 'yes' ) :
?>
    <p>This content appears only when the field is exactly <code>yes</code>.</p>
<?php endif; ?>

This avoids treating values such as 0, false, or an empty value as equivalent to a deliberate setting. If your field stores an array or another complex value, define and test the structure you expect rather than applying a scalar comparison.

Key existence is a different test

The expression $field_value !== '' means the single value returned is not an empty string. It is not a universal “metadata key exists” test. An existing key with an empty string, a zero-like value, or a serialized structure may require a different rule. Confirm the field’s stored representation and apply a condition that matches it; see the return-value notes in the WordPress function reference.

Filter a list so only matching posts are returned

When the whole post should be absent from a list, put the condition in WP_Query rather than hiding its markup after the query has already returned it. WordPress expects meta_query clauses as nested arrays, even when there is only one clause.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Require that a metadata key exists

<?php
$query = new WP_Query(
    array(
        'meta_query' => array(
            array(
                'key'     => 'your_field_key',
                'compare' => 'EXISTS',
            ),
        ),
    )
);

if ( $query->have_posts() ) :
    while ( $query->have_posts() ) :
        $query->the_post();
        // Output each matching post.
    endwhile;
    wp_reset_postdata();
endif;
?>

The EXISTS comparison requires the metadata key to be present. The WP_Query reference also documents NOT EXISTS, key/value parameters, and other comparison operators: WP_Query reference.

Require a particular value

For a field that must contain a specific value, include value and the appropriate comparison:

<?php
$query = new WP_Query(
    array(
        'meta_query' => array(
            array(
                'key'     => 'your_field_key',
                'value'   => 'yes',
                'compare' => '=',
            ),
        ),
    )
);
?>

Use the exact spelling, capitalization, and data format used by your field configuration. If “present” actually means “non-empty,” model that requirement explicitly and test it against the site’s data rather than assuming key existence implies a useful value.

Reset post data after a secondary query

A custom query changes the global post context while its loop runs. After outputting its posts, call wp_reset_postdata() as shown above so the surrounding template returns to the main post. The Loop handbook explains the normal Loop context and post-data handling: The Loop handbook.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Using the block editor’s Query Loop

The Query Loop block provides controls for post type, filters, ordering, result count, and the repeated Post Template layout. Its official user guide does not document a native control for “metadata key exists” or an equivalent custom-field condition: Query Loop block documentation.

If the block interface cannot express your requirement, use a PHP query/template customization or a plugin that explicitly supports post-meta filtering. Capabilities vary by theme and installed plugins, so verify the resulting query in the actual site environment.

Common mistakes and checks

  • Checking the wrong scope: a template condition affects the current post’s markup; it does not filter an archive’s result set.
  • Using a conditional tag for metadata: functions such as is_single() describe the current request context, not whether a custom-field key exists. Conditional tags should be used only after the query is set up or within the appropriate hook. See Conditional Tags handbook and the List of Conditional Tags.
  • Assuming truthiness is sufficient: zero, false-like values, empty strings, and complex values can have different meanings. Compare the stored value or structure deliberately.
  • Using the wrong key: custom-field labels shown in an editor are not always the database metadata key. Confirm the actual key used by the field configuration.
  • Forgetting secondary-query cleanup: call wp_reset_postdata() after a custom loop before rendering content that belongs to the main query.
  • Expecting a hidden section to improve list results: hiding markup does not prevent the post from being fetched. Apply the metadata clause to the query when exclusion is required.

A practical decision checklist

  1. Write down the exact metadata key.
  2. Choose the scope: current-post markup or the set of posts returned by a query.
  3. Define the data rule: key exists, value is non-empty, or value equals a specified representation.
  4. Implement the matching API: get_post_meta() in the template or meta_query in WP_Query.
  5. Test posts with the key, without the key, with an empty value, and with any meaningful zero/false-like or complex values.
  6. If a secondary query is used, reset post data before the rest of the template runs.

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.