简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM