Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Hello this is my query. I want to show the titles of posts that have fullfil the following but it doesn't show anything, I don't know. Thank you! Also, is there another way to show the results, more wordpress-like?

<?php 

$theposts = $wpdb->get_results("SELECT post_title FROM $wpdb->posts
    WHERE ID IN
(
   SELECT DISTINCT post_id
   FROM $wpdb->postmeta
   WHERE meta_key = 'extra1' AND meta_value = 'test'
)");

foreach ($theposts as $thepost) {
echo $thepost->post_title;

}
?>
share|improve this question

1 Answer 1

You need to use WordPress meta query:

Display posts where the custom field key is 'extra1' and the custom field value is 'test':

$query = new WP_Query( array( 'meta_key' => 'extra1', 'meta_value' => 'test' ) );

For more info take a look at WP Query Custom_Field_Parameters.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.