Would you like to display a WordPress post only if it has a specific custom field?
Custom fields are a handy WordPress feature that allows you to add additional information to your WordPress posts and pages. You might like to filter your content by your custom fields.
In this article, we will show you how to display a WordPress post only if it has a specific custom field.
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, including using the Advanced Custom Fields (ACF) plugin.
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. 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.
With that being said, let’s take a look at how to display a WordPress post only if it has a specific custom field.
Displaying a WordPress Post Only if It Has a Specific Custom Field
To follow this guide, you’ll have to add code to your WordPress theme’s files. If you haven’t done this before, then see our guide on how to copy and paste code in WordPress.
You need to have a fair understanding of how WordPress loops work because we will call these parameters in a WordPress query, so this tutorial isn’t suitable for complete beginners.
You need to paste the following loop code snippet wherever you want to display the list of posts with the custom field. Most likely, that will be in a custom WordPress page template:
<?php
// The Query to show a specific Custom Field
$the_query = new WP_Query('meta_key=color');
// The Loop
while ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
the_content();
endwhile;
// Reset Post Data
wp_reset_postdata();
?>
This example code only shows posts that have a custom field value of ‘color’ regardless of what that color value is. Don’t forget to change ‘color’ on line 5 to your custom field name.
Now, if you want to show specific posts that have a custom field with a specific value, then you just have to change the query like this:
$the_query = new WP_Query( 'meta_value=blue' );
This will display all posts that have a ‘blue’ value in any custom field.
If you want to make sure that the ‘color’ field has a ‘blue’ value, then your query code will look like this:
$the_query = new WP_Query( array( 'meta_key' => 'color', 'meta_value' => 'blue' ) );
You can learn about additional parameters you can use in your query on the WordPress WP_Query code reference page.
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.
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 WordPress SEO guide or check out our list of the best WordPress plugins to grow your website.
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 says
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 says
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?
Максим Каминский says
great thanks, it help wery well!
pjhooker says
Thx!
Eduard Unruh says
omg finally THANKS!
Mario M says
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 says
Wonderful, just so simple and clean.
Thank you.
scottlee.me says
@ad Great question! I’m curious too.
ad says
Hi,
How could I show posts that DON’T have a specific Custom Field? Any idea?
Thanks!!!
tara tin says
as I know from php it must be meta_key!=’your key’
;just you need to know that “!” means “not”
brunobruno2 says
Beatiful! Many thanks for sharing it. Works like a charm.