You know that moment 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.
📌 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 information about the post, such as the title, author, and publish date.
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.
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 back 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:
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.
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.
We recommend understanding how the WordPress template hierarchy works so that you know where to add the code later.
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: