Question

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');
Was it helpful?

Solution

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.

OTHER TIPS

 $querystr = "
    SELECT wposts.* 
    FROM $wpdb->posts wposts, $wpdb->postmeta wpostmeta
    WHERE wposts.ID = wpostmeta.post_id 
    AND wpostmeta.meta_key = 'custom-key' 
    AND wposts.post_type = 'page' 
    ORDER BY wpostmeta.meta_value DESC
    ";
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top