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’ll 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 you’ll find lots of helpful tips 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 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.
You need to paste the following loop code 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 called ‘color’ regardless of what that color value is. Don’t forget to change ‘color’ on line 5 to the name of your custom field.
Now if you want to show 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.
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 learn how to add web push notifications to your site, or check out our list of the best social media 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.
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
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!
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’);
Wonderful, just so simple and clean.
@ad Great question! I’m curious too.
How could I show posts that DON’T have a specific Custom Field? Any idea?
tara tin says
as I know from php it must be meta_key!=’your key’
;just you need to know that “!” means “not”
Beatiful! Many thanks for sharing it. Works like a charm.