What’s actually slowing this PC down?

Pick the symptom - the matching free tool is one click away.

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

Some links on this page are affiliate links: if you buy through them we may earn a commission, at no extra cost to you.

Use a template check when you want to hide part of the current post, or a meta_query when you want to exclude entire posts from a list. First decide what “has the field” means for your site: the metadata key exists, its value is non-empty, or its value matches a specific choice.

Choose whether to hide content or filter the post list

What you want Where to check Result
Show or hide a section on the current post Template code in the current post’s Loop The post remains wherever it is listed; only the relevant markup is conditional.
Show only posts that meet a metadata condition The query that retrieves the list, using meta_query Posts that do not meet the condition are excluded from that result set.

These approaches solve different problems. A check in a post template does not remove that post from archives or a Query Loop; a query filter affects only the results of the query where it is applied.

Show a section only when the current post’s field is populated

In a theme template running inside the Loop, retrieve the current post’s value and conditionally print the markup. For a field whose acceptable value is any non-empty string:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<?php
$field_value = get_post_meta( get_the_ID(), 'your_field_key', true );

if ( $field_value !== '' ) {
    // Output the markup that should appear only when the field is populated.
}
?>

Replace your_field_key with the exact metadata key used by your site. The third argument, true, asks get_post_meta() for a single value. This condition means that the returned value is not the empty string; it is not a general test for whether a key exists. See the get_post_meta() reference for return-value details.

Match the check to the value your field stores

  • Any non-empty string: compare against '' as in the example, if an empty string is the only value you consider unpopulated.
  • A specific choice: compare explicitly. For a field stored as the string yes, use if ( $field_value === 'yes' ).
  • Zero, false-like, or structured data: do not rely on a generic truthiness test. Decide how that field is stored and what should count as populated, then check that representation deliberately.

The current post ID matters: this code belongs in the right post context, usually inside the Loop, so it checks the post being rendered. WordPress describes the Loop as the theme mechanism for outputting posts and their template data in its Loop handbook.

Exclude posts that lack the field from a list

Apply the condition to the query that builds the list. WordPress documents custom-field parameters, including meta_query and the EXISTS comparison, in the WP_Query reference. For example, to require the key to exist:

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

Replace the key with your actual metadata key. The nested arrays are the expected shape even for a single meta_query clause. A key-existence clause answers a different question from “does the field contain a non-empty value?” Test the query against your site’s actual metadata and WordPress setup before using it for a live list.

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

Require a particular value instead

If only a specific stored value should qualify, use a comparison and value that match the field’s data. For example, if the field stores the string yes, the query condition needs to test for that value rather than merely requiring the key to exist. The precise comparison depends on the stored value and field configuration; do not assume every custom field uses the same representation.

What to expect in the block editor’s Query Loop

The Query Loop block provides controls for selecting posts and arranging their repeated Post Template layout. The official Query Loop block guide does not document a built-in control for filtering by custom-metadata existence. If the whole list must be filtered by post meta, you may need a PHP query or template customization, or a compatible plugin; available options depend on the active theme and installed plugins.

A template-level conditional can still hide a section within each displayed post, but it does not make the Query Loop omit posts without that field.

Keep custom-field checks separate from WordPress conditional tags

Tags such as is_single() describe the current queried page or context; they do not test whether a post has a custom metadata key. WordPress covers these context checks in its Conditional Tags handbook and List of Conditional Tags. Conditional query tags should be used only after the query is set up or within an appropriate action hook.

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

Check the post context when using a secondary query

If you create a secondary WP_Query and loop through its posts, make sure the metadata check uses the post currently being iterated. Follow WordPress’s documented post-data reset guidance when appropriate so later template code returns to the main query’s post context; the WP_Query reference documents the query API and related usage.

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.