Trusted WordPress tutorials, when you need them most.
Beginner’s Guide to WordPress
WPB Cup
25 Million+
Websites using our plugins
16+
Years of WordPress experience
3000+
WordPress tutorials
by experts

How to Display a WordPress Post Only if It Has a Specific Custom Field

You know when you need to show certain WordPress posts based on custom fields? We’ve been there.

When we first started with WordPress, seeing any code snippet felt intimidating, especially when all we wanted was to display posts with specific information.

But here’s the good news: after helping thousands of WordPress beginners, we’ve found that even something that sounds technical, like custom field filtering, doesn’t have to be scary.

In fact, we’ll walk you through a simple code method to do it.

This solution works perfectly whether you’re publishing regular blog posts or custom post types based on specific criteria.

How to Display a WordPress Post Only if It Has a Specific Custom Field

📌 Quick Note: This tutorial is for displaying WordPress posts if they have a value entered in a specific custom field.

If you want to display custom fields on the front end of a WordPress post, then you can read our guide on how to display custom fields in WordPress themes.

Why Display WordPress Posts With a Specific Custom Field?

When you create a post on your WordPress website, you can use custom fields to add additional metadata to the post.

Metadata is extra information you can add to a post. For example, if you were writing a book review, you could add custom fields for ‘Author’s Name’ or ‘Star Rating’.

Custom fields are an advanced WordPress concept, and there are many ways to add custom fields in WordPress. You’ll find lots of helpful tips on how to use and display custom fields in our post, WordPress Custom Fields 101: Tips, Tricks, and Hacks.

You Can Add Metadata to a Post Using Custom Fields

One of our users asked us how to display WordPress posts only if a specific custom field was present. This may be useful if you’re looking to create a custom page that lists all of the posts that contain a specific custom field and/or value.

After replying with the answer, we thought it would be best to share it with everyone else so the larger WordPress.org community can benefit from it as well.

Editing Your WordPress Theme Files: What to Keep in Mind

To be able to follow this tutorial properly, here are some things you should keep in mind:

  1. This tutorial involves editing your WordPress theme files with code, so it’s not the most suitable for complete beginners. If you’re new to this, then you will need to read our guide on how to copy and paste code in WordPress.
  2. We recommend backing up your website and/or using a staging environment so that your live site doesn’t get affected when an error occurs. This is because you’ll be adding code to your theme files, which can be risky.
  3. We recommend understanding how the WordPress template hierarchy works so that you know where to add the code later.
  4. You will also need to get familiar with how WordPress loops work because we will call these parameters in a WordPress query.

Also, note that this tutorial only works with classic WordPress themes, as block themes have a different set of theme files.

With that being said, let’s take a look at how to display a WordPress post only if it has a specific custom field.

How to Filter Your Posts Based on Specific Custom Fields

Before we show you the code you need to use, you need to know which theme file you need to add it to. Most likely, that will be a page template, such as index.php, archive.php, or page.php.

Let’s say you want to add it to the index.php file of the Twenty Twenty-One theme. Here is what the WordPress loop part of that file looks like at the moment:

<?php
if ( have_posts() ) {

// Load posts loop.
	while ( have_posts() ) {
		the_post();

		get_template_part( 'template-parts/content/content', get_theme_mod( 'display_excerpt_or_full_post', 'excerpt' ) );
	}

	// Previous/next page navigation.
	twenty_twenty_one_the_posts_navigation();

} else {

	// If no content, include the "No posts found" template.
	get_template_part( 'template-parts/content/content-none' );

}

get_footer();

This code uses the default WordPress loop (have_posts() and the_post()) to display posts. This method is suitable for most standard WordPress themes and is used to display posts without any custom filtering or sorting.

Now, let’s say you use the custom field ‘color’ like in the example above. You will need to replace that entire code with the snippet below:

<?php
// The Query to show a specific Custom Field
$the_query = new WP_Query('meta_key=color');

// Load posts loop.
if ( $the_query->have_posts() ) {
    while ( $the_query->have_posts() ) {
        $the_query->the_post();

        get_template_part( 'template-parts/content/content', get_theme_mod( 'display_excerpt_or_full_post', 'excerpt' ) );
    }

    // Previous/next page navigation.
    twenty_twenty_one_the_posts_navigation();

} else {

    // If no content, include the "No posts found" template.
    get_template_part( 'template-parts/content/content-none' );

}

// Reset Post Data
wp_reset_postdata();

get_footer();

In this new code, we introduced a custom query to fetch posts that have a specific custom field (in this case, any post with the custom field ‘color’).

It then uses a custom loop (if ($the_query->have_posts())) to iterate over the posts fetched by this custom query, displaying each post’s content in the same way as the first snippet.

We also added thewp_reset_postdata() function to ensure that WordPress returns to displaying all posts correctly after the custom query. This ensures the site functions smoothly and shows the right content to users.

Now, what if you want to find posts where the ‘color’ custom field has a specific value, like ‘blue’? To do that, you just need to add a ‘meta_value’ parameter to your query.

Your updated query code will look like this:

$the_query = new WP_Query( array( 'meta_key' => 'color', 'meta_value' => 'blue' ) );

💡Pro Tip: You can take your filtering even further with the meta_compare parameter. This lets you find posts that don’t match a value.

For example, to display all posts where the color is not ‘blue’, your query would look like this:

$the_query = new WP_Query( array( 'meta_key' => 'color', 'meta_value' => 'blue', 'meta_compare' => '!=' ) );

The != tells WordPress to find all posts where the ‘color’ field exists but is not equal to ‘blue’.

There are many other comparison operators you can use, which you can find in the official WordPress developer documentation.

Once you have saved the code in the page template PHP file, you can check your WordPress site on the front end to see your code in action.

Frequently Asked Questions About Displaying a Post If It Has a Specific Custom Field

Here are some questions that our readers frequently ask about displaying a post if it has only a specific custom field:

Is there a way to do this without editing theme files?

Yes, and it’s the method we recommend for most users. A plugin like WPCode allows you to safely add custom PHP snippets like this without directly touching your theme’s files.

This protects your site from errors and ensures your customizations aren’t lost when you update your theme.

Can I filter posts by multiple custom fields?

You can. The WP_Query function is very powerful and accepts more complex arguments. You would use a ‘meta_query’ parameter, which lets you build an array of conditions to match multiple custom fields at once.

Does this method work with block themes?

This specific tutorial focuses on classic themes by editing files like index.php or archive.php. Block themes use the Site Editor and don’t rely on these files.

To achieve a similar result in a block theme, you would use the Query Loop block and its built-in filtering options, which don’t require custom code.

What’s the difference between ‘meta_key’ and ‘meta_value’?

Think of the ‘meta_key’ as the name of the label, like ‘Color’ or ‘Status.’ The ‘meta_value’ is the actual data entered into that field, such as ‘Blue’ or ‘Published.’

Using the key finds any post with that custom field, while using the value finds any post with that specific piece of information.

Learn More Ways to Customize Your WordPress Site

Here are more ways you can customize your WordPress pages:

We hope this tutorial helped you learn how to display a WordPress post only if it has a specific custom field. You may also want to see our complete guide on how to edit a WordPress website and our expert picks of the best Figma plugins for WordPress.

If you liked this article, then please subscribe to our YouTube Channel for WordPress video tutorials. You can also find us on Twitter and Facebook.

Disclosure: Our content is reader-supported. This means if you click on some of our links, then we may earn a commission. See how WPBeginner is funded, why it matters, and how you can support us. Here's our editorial process.

The Ultimate WordPress Toolkit

Get FREE access to our toolkit - a collection of WordPress related products and resources that every professional should have!

Reader Interactions

11 CommentsLeave a Reply

  1. i have a question sir if i want to display single post and custom fields then what i should do.
    i dont want to display the post with specific custom fields. i want to display all custom fields of post

  2. Hi, thank for the useful tutorial. I have a question, in a wordpress website i set a meta value named “meta_country” and then i set every post with the country of the article, like “us”, “uk”, “fr”… Now I’m trying to add somewhere in the home of the blog a link that show list of all post with a specific country and a specific tag. For example all “UK” post tagged “APPLE”.
    I don’t understand how to do that, someone could help me?

  3. I wasnt able to generate any results unless I included “post_type” parameter into the query.

    ie: $the_query = new WP_Query(‘post_type=page&meta_key=color’);

    • as I know from php it must be meta_key!=’your key’

      ;just you need to know that “!” means “not”

Leave A Reply

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.