WP_Query With meta_query and Value of Custom Field as 0

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?

Related posts

Leave a Reply

2 comments

  1. 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.