You know when you need to display a WordPress post only if it has a specific custom field? 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.

💡 TL;DR: You can show only WordPress posts that have a specific custom field by using a custom WP_Query in your theme files (like index.php) to filter those posts.
📌 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.

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
Before you start, there are a few important things you should keep in mind when working with theme files.
If you add code directly to your theme files, then your changes will be lost the next time you update your theme.
That’s why we recommend using a plugin like WPCode to safely add custom PHP code without editing your theme. If you prefer editing theme files, then make sure you are using a child theme so your changes are preserved.
To be able to follow this tutorial properly, here are some things you should keep in mind:
- This tutorial involves adding code to your WordPress site, 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.
📍Important Note: This tutorial only works with classic WordPress themes. Block themes use the Site Editor and a different approach to display content, so this method of editing template files will not apply.
With that being said, let’s take a look at how to display a WordPress post only if it has a specific custom field.
- Step 1: Install a Code Snippet Plugin
- Step 2: Add the Custom Query Code
- Step 3: Filter by Specific Meta Value (Optional)
- Step 4: Filter by Meta Comparison (Optional)
- Step 5: Filter Posts by Multiple Custom Fields (meta_query)
- Real-World Example: Show Only 5-Star Reviews
- Frequently Asked Questions About Displaying a Post If It Has a Specific Custom Field
- Frequently Asked Questions About Displaying a Post If It Has a Specific Custom Field
- Learn More Ways to Customize Your WordPress Site
Step 1: Install a Code Snippet Plugin
Some tutorials will tell you to open your theme’s index.php file and completely rewrite the WordPress loop using something called WP_Query. We do not recommend doing this.
Modifying the main template files can break your site’s layout, cause duplicate database queries that slow down your site, and completely break your pagination (the “Next Page” buttons).
Instead, the safest and most efficient way to do this is by using the pre_get_posts action hook. This tells WordPress to filter the posts before it even loads the page template.
To add this custom code, we recommend using a plugin like WPCode. It allows you to safely add PHP code without directly touching your theme’s files, meaning your site won’t break if you make a mistake, and your changes won’t be lost when your theme updates.
Step 2: Add the Custom Query Code
Once you have installed and activated WPCode, navigate to Code Snippets » Add Snippet in your WordPress dashboard and select ‘Add Your Custom Code (New Snippet)’.

Make sure the “Code Type” is set to PHP Snippet.
Then, paste in the following code:
<?php
add_action( 'pre_get_posts', 'wpb_filter_posts_by_custom_field' );
function wpb_filter_posts_by_custom_field( $query ) {
// Only modify the main query on the front-end homepage
if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) {
// Tell WordPress to only get posts with the custom field key 'color'
$query->set( 'meta_key', 'color' );
}
}
First, the code checks to make sure it is only altering the main query on the front end of your website (specifically the blog homepage), leaving your WordPress admin dashboard completely untouched.
Then, it uses $query->set to tell WordPress to only retrieve posts that have the custom field ‘color’ attached to them.
Because this code modifies the query before the page loads, your theme’s default pagination will work perfectly without any extra code! Simply toggle the WPCode switch to Active and click the ‘Save Snippet’ button to make it live.
Step 3: Filter by Specific Meta Value (Optional)
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 rule to your code.
Your updated snippet will look like this:
<?php
add_action( 'pre_get_posts', 'wpb_filter_posts_by_custom_field_value' );
function wpb_filter_posts_by_custom_field_value( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) {
$query->set( 'meta_key', 'color' );
$query->set( 'meta_value', 'blue' );
}
}
Step 4: Filter by Meta Comparison (Optional)
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:
<?php
add_action( 'pre_get_posts', 'wpb_filter_posts_by_meta_compare' );
function wpb_filter_posts_by_meta_compare( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) {
$query->set( 'meta_key', 'color' );
$query->set( 'meta_value', 'blue' );
$query->set( '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.
Step 5: Filter Posts by Multiple Custom Fields (meta_query)
For more complex scenarios where you need to filter posts based on multiple custom fields or combine conditions (like AND/OR), you’ll use the meta_query array.
This allows you to define an array of conditions, each targeting a different custom field or value.
For example, to display posts with the color ‘blue’ and size ‘large,’ your code would look like this:
<?php
add_action( 'pre_get_posts', 'wpb_filter_posts_by_multiple_fields' );
function wpb_filter_posts_by_multiple_fields( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) {
$meta_query = array(
'relation' => 'AND', // or 'OR'
array(
'key' => 'color',
'value' => 'blue',
'compare' => '=',
),
array(
'key' => 'size',
'value' => 'large',
'compare' => '=',
),
);
$query->set( 'meta_query', $meta_query );
}
}
This query will retrieve posts that have both the custom field ‘color’ set to ‘blue’ AND the custom field ‘size’ set to ‘large’.
You can adjust the 'relation' parameter to ‘OR’ to find posts matching at least one of the conditions.
Real-World Example: Show Only 5-Star Reviews
Let’s say you run a WordPress book review site and you use a custom field called star_rating to store review scores.
You can easily display only posts that have a 5-star rating on your blog homepage using this query:
<?php
add_action( 'pre_get_posts', 'wpb_show_only_five_star_reviews' );
function wpb_show_only_five_star_reviews( $query ) {
if ( ! is_admin() && $query->is_main_query() && $query->is_home() ) {
$query->set( 'meta_key', 'star_rating' );
$query->set( 'meta_value', '5' );
}
}
This is extremely useful if you want to ensure your main feed only features your highest-rated content.
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:
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! In fact, the pre_get_posts method we outline in this tutorial is specifically designed so you don’t have to edit your theme files. By using a snippet plugin like WPCode, you can safely apply this filter to your site without touching a single line of your theme’s core code.
Can I filter posts by multiple custom fields?
You can. The pre_get_posts hook accepts complex arguments. You would use a ‘meta_query’ array, which lets you build a list of conditions to match multiple custom fields at once.
Does this method work with block themes?
While the pre_get_posts hook can technically interact with block themes, the native WordPress Query Loop block does not have a built-in interface for custom field filtering out of the box. If you are using a Full Site Editing block theme, the easiest approach is to use a plugin that adds custom field filtering to the Query Loop block, rather than writing custom PHP.
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 attached to it, while using the value filters it down to only find posts with that specific piece of information.
Learn More Ways to Customize Your WordPress Site
Here are more ways you can customize your WordPress pages:
- How to Style Individual Categories Differently in WordPress
- How to Add a Custom Scrollbar in WordPress
- How to Add Custom Styles to WordPress Widgets
- How to Style Each WordPress Post Differently
- How to Use Shortcodes in Your WordPress Themes
- How to Highlight New Posts for Returning Visitors in WordPress
- How to Change the Sidebar Side in WordPress
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.

ahmed
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
Dave101
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?
Максим Каминский
great thanks, it help wery well!
pjhooker
Thx!
Eduard Unruh
omg finally THANKS!
Mario M
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’);
sacha
Wonderful, just so simple and clean.
Thank you.
scottlee.me
@ad Great question! I’m curious too.
ad
Hi,
How could I show posts that DON’T have a specific Custom Field? Any idea?
Thanks!!!
tara tin
as I know from php it must be meta_key!=’your key’
;just you need to know that “!” means “not”
brunobruno2
Beatiful! Many thanks for sharing it. Works like a charm.