So I have created a meta box for a review rating. Sometimes the value of the rating (out of 5) is zero.

When you enter a 0 into the input and save the data, it doesn't process because of the fact 0 also means false in the statement:

if ($meta_value) ...

I was wondering if anyone knew a way to save the zero value?

The only answer I have found is to enter 0x0 and then convert it (as per: https://wordpress.stackexchange.com/a/96536/17411), but for users it seems like a confusing task to explain, and then for them to remember..

This is my save_meta:

function save_custom_meta($post_id) {
    global $meta_review_fields_1;

// Verify nonce
if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__)))   
    return $post_id;  

// Check autosave
    return $post_id;

// Check permissions
if ('page' == $_POST['post_type']) {
    if (!current_user_can('edit_page', $post_id))
        return $post_id;
    } elseif (!current_user_can('edit_post', $post_id)) {
        return $post_id;

// Loop and save: Review
foreach ($meta_review_fields_1 as $field_1) {
    $old_1 = get_post_meta($post_id, $field_1['id'], true);
    $new_1 = $_POST[$field_1['id']];
    if ($new_1 && $new_1 != $old_1) {
        update_post_meta($post_id, $field_1['id'], $new_1);
    } elseif ('' == $new_1 && $old_1) {
        delete_post_meta($post_id, $field_1['id'], $old_1);

Mind you, I need it to delete the content of other fields if empty, just the rating input that should allow the value of 0.

  • How can 0 be a rating, surely 0 is the equivalent to nothing, ie. no rating, no? If someone said rate a movie from 1 - 10, it wouldn't make any sense if they said 0, would it? Just curious on your reasoning for considering 0 a valid rating.
    – t31os
    Commented May 3, 2013 at 10:02
  • Sorry, its an average rating. So there are 5 criteria for the rating. If they really fail an aspect then they get a 0. It is not uncommon to get zeroes.
    – markb
    Commented May 3, 2013 at 10:34
  • Ah hah, i knew i was missing something, naturally there are cases where a zero does make sense. :)
    – t31os
    Commented May 3, 2013 at 11:27
  • An alternative would be a dropdown or radio buttons.
    – brasofilo
    Commented May 3, 2013 at 15:16
  • Would a elseif ('' == $new_1 && $old_1 || 0 === &new_1) { work? Or would it cause errors later on?
    – markb
    Commented May 4, 2013 at 5:32

1 Answer 1


Your issue seems to be with too loose logic check, rather than saving.

To treat valid but falsy value correctly you need something like this:

if ( false !== $meta_value )

This will strictly match only the case when value doesn't contain false (return of the API when fetch failed).

  • At which line would this go? if ($new_1 && $new_1 != $old_1) {?
    – markb
    Commented May 3, 2013 at 11:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.