Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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: http://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
if (defined('DOING_AUTOSAVE') && DOING_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.

share|improve this question
    
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 May 3 '13 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. –  Ahhhhhhhhhhhhhdfgbv May 3 '13 at 10:34
    
Ah hah, i knew i was missing something, naturally there are cases where a zero does make sense. :) –  t31os May 3 '13 at 11:27
    
An alternative would be a dropdown or radio buttons. –  brasofilo May 3 '13 at 15:16
    
Would a elseif ('' == $new_1 && $old_1 || 0 === &new_1) { work? Or would it cause errors later on? –  Ahhhhhhhhhhhhhdfgbv May 4 '13 at 5:32
add comment

1 Answer

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

share|improve this answer
    
At which line would this go? if ($new_1 && $new_1 != $old_1) {? –  Ahhhhhhhhhhhhhdfgbv May 3 '13 at 11:44
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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