Beginner's Guide for WordPress / Start your WordPress Blog in minutes

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

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.

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.

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. 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.

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.