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.

I have a problem with the update_post_meta function.
I have a user submitted value, which I pass via $_POST and then saving to post meta.

All is working fine, but when the value is '0' the post meta is not updated.

My code:

$id = absint($_POST['ex_id']);
$key = (string) $_POST['id'];
$value = (string) $_POST['value'];

echo update_post_meta($id, 'wpptabs_' . $key, $value);

Does anyone have any idea what might be wrong?

share|improve this question
1  
Can you elaborate on "until the value is not '0' (other digits work fine)"? Isn't that a contradiction? Which value are you referring to? You also might want to show your code in more context. –  vancoder Apr 19 '13 at 21:46
    
I have corrected the sentence to be more clear. However I do not have more code. There is a form which passes values via $_POST. And here is the whole code that fetches it on the other side. –  HoGo Apr 19 '13 at 21:50
3  
Of course there is more code. These lines don't exist in a vacuum. What file are they in? Is this a plugin? Functions.php? A theme? What triggers this code? Is $value actually passing 0, or is it an unchecked checkbox - in which case it may not be passing anything at all. –  vancoder Apr 19 '13 at 21:59
    
0 will resolve to false under some circumstances. That may be part of the problem, but as @vancoder has requested, we need more code. –  s_ha_dum Apr 20 '13 at 0:22

2 Answers 2

update_post_meta() calls update_metadata which begins with this line:

if ( !$meta_type || !$meta_key )
    return false;

I wrote a simple test (with a lot of cut & paste) to see how string values of 0 are evaluated (instead of looking it up in the PHP manual):

<?php

$meta_key = (string) 0;

if ( ! $meta_key )
    echo "0 exits function<br />";

else {
    echo "0 remains in function<br />";
    echo "&nbsp;&nbsp;\$meta_key = $meta_key<br />";
    echo "&nbsp;&nbsp;(integer) \$meta_key = " . (integer) $meta_key . '<br />';
}

echo '<br />';
$meta_key = (string) 0;

if ( ! $meta_key )
    echo "(string) 0 exits function<br />";

else {
    echo "(string) 0 remains in function<br />";
    echo "&nbsp;&nbsp;\$meta_key = $meta_key<br />";
    echo "&nbsp;&nbsp;(integer) \$meta_key = " . (integer) $meta_key . '<br />';
}

echo '<br />';
$meta_key = (string) '0';

if ( ! $meta_key )
    echo "(string) '0' exits function<br />";

else {
    echo "(string) '0' remains in function<br />";
    echo "&nbsp;&nbsp;\$meta_key = $meta_key<br />";
    echo "&nbsp;&nbsp;(integer) \$meta_key = " . (integer) $meta_key . '<br />';
}

echo '<br />';
$meta_key = 0x0;

if ( ! $meta_key )
    echo "0x0 exits function<br />";

else {
    echo "0x0 remains in function<br />";
    echo "&nbsp;&nbsp;\$meta_key = $meta_key<br />";
    echo "&nbsp;&nbsp;(integer) \$meta_key = " . (integer) $meta_key . '<br />';
}

echo '<br />';
$meta_key = (string) 0x0;

if ( ! $meta_key )
    echo "(string) 0x0 exits function<br />";

else {
    echo "(string) 0x0 remains in function<br />";
    echo "&nbsp;&nbsp;\$meta_key = $meta_key<br />";
    echo "&nbsp;&nbsp;(integer) \$meta_key = " . (integer) $meta_key . '<br />';
}

echo '<br />';
$meta_key = '0x0';

if ( ! $meta_key )
    echo "'0x0' exits function<br />";

else {
    echo "'0x0' remains in function<br />";
    echo "&nbsp;&nbsp;\$meta_key = $meta_key<br />";
    echo "&nbsp;&nbsp;(integer) \$meta_key = " . (integer) $meta_key . '<br />';
}

echo '<br />';
$meta_key = (string) '0x0';

if ( ! $meta_key )
    echo "(string) '0x0' exits function<br />";

else {
    echo "(string) '0x0' remains in function<br />";
    echo "&nbsp;&nbsp;\$meta_key = $meta_key<br />";
    echo "&nbsp;&nbsp;(integer) \$meta_key = " . (integer) $meta_key . '<br />';
}

The result was:

0 exits function

(string) 0 exits function

(string) '0' exits function

0x0 exits function

(string) 0x0 exits function

'0x0' remains in function
  $meta_key = 0x0
  (integer) $meta_key = 0

(string) '0x0' remains in function
  $meta_key = 0x0
  (integer) $meta_key = 0

So, to add a value of 0 you could change it to the string '0x0' and cast it to integer when retrieving it. 0x0 is binary (I think) for 0.

share|improve this answer
1  
0 is one of the values considered false when you evaluate it in an if statement. –  Manny Fleurmond Apr 20 '13 at 10:54

PHP treats 0 as equivalent to false for comparisons. Because the update_post_meta function checks for a value before saving, it's exiting because 0 == false.

You could do something like this before saving the post meta, it's a bit hacky but it would work:

if ( 0 === $value ) {
    $value = 'zero';
}

Then when you're retrieving your values later, you just do the opposite:

$value = get_post_meta( $post_id, 'meta-key', true );
if ( 'zero' == $value ) {
    $value = 0;
}
share|improve this answer

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.