Menu

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

February 4, 2014 - wordpress

1)

<?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();

?>

2)

Now if you want to show posts that has a custom field with a specific value, then you just have to change the query like this:

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

Now if you want to stress out the key and value for example you only want to pull posts that has a custom field key color and the value as blue, then your query code will look liks this:

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

Leave a Reply