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.