BlogThemes

How to Display a WordPress Post only if it has a specific Custom Field

Lately one in all our customers requested us show WordPress posts provided that a particular customized area was current. After replying again with the reply, we thought it might finest if we share with everybody else, so the bigger group can profit from it as properly.

It’s essential have a good understanding of how WordPress loops work as a result of we’ll name these parameters in a WordPress question.

The instance code under will solely present posts which have a customized area colour current it doesn’t matter what worth the colour area has. You would wish to stick this loop code wherever you wish to posts to indicate. More than likely in a customized WordPress web page template.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
// The Question to indicate a particular Customized Discipline
$the_query = new WP_Query('meta_key=colour');
// The Loop
whereas ( $the_query->have_posts() ) : $the_query->the_post();
the_title();
the_content();
endwhile;
// Reset Publish Knowledge
wp_reset_postdata();
?>

Now if you wish to present posts that has a customized area with a particular worth, then you definitely simply have to vary the question like this:

1
$the_query = new WP_Query( 'meta_value=blue' );

Now if you wish to stress out the important thing and worth for instance you solely wish to pull posts that has a customized area key colour and the worth as blue, then your question code will look liks this:

1
$the_query = new WP_Query( array( 'meta_key' => 'colour', 'meta_value' => 'blue' ) );

There are much more customized parameters that you need to use whereas working in your websites. Simply seek advice from the Codex web page for WP_Query Parameters.

Download The WPSaviour App Now

Related posts
Beginner’s GuideBest WordPress PluginsBlog

9 Best Social Proof Plugins for WordPress & WooCommerce (2020)

Tips

How To Disable The WordPress User Admin Toolbar

Tips

WordPress Front Page vs Homepage

Tips

5 Things You Must Avoid Doing in WordPress

Leave a Reply

Your email address will not be published.

Pay in your preferred currency
Indian rupee
Euro