Filter WP_Query for posts having a certain meta-value

How to filter WP_Query for posts having a certain meta-value, without using a Custom Select Query?

I have a custom posttype with meta-key: “open”, and meta-value options: “yes” or “no”.

I would like to show posts only with meta_value = yes, for meta_key = “open”.

function filter_where($where="") {    

    $open = "yes";

    //$where .= " AND post_date > '" . date('Y-m-d', strtotime('-2 days')) . "'";
    return $where;
}
add_filter('posts_where', 'filter_where');

2 s
2

I am not sure from your wording if you hadn’t tried it with query argument or it didn’t work?

$the_query = new WP_Query(array( 'meta_key' => 'open', 'meta_value' => 'yes' ));

Custom Field Parameters in Codex.

Leave a Comment