I am experiencing some unexpected behavior. I have added a custom filed of “Rating” to my posts. I want to count up the number of posts with a certain rating:
$args = array( 'post_status' => 'publish', 'meta_query' => array( array( 'key' => 'rating', 'compare' => '=', 'value' => 7 ) ) ); $posts = new WP_Query($args); echo $posts->post_count;
When I set the value to 7, it echos the correct number of posts for that rating. But for some reason, if I set the value to 0, it echos the total number of published posts. What am I missing?
I did a little digging and this appears to be dude to PHP’s “empty” function. This is slated to be fixed in WordPress 3.2. Details here: http://core.trac.wordpress.org/ticket/15292
I don’t know exactly why, but when I need it I just use compare = ‘<=’ value = ‘0.5’
I think wordpress use 0 as empty or null var, instead of actually zero.