5

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?

4
  • 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
    Commented Apr 19, 2013 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
    Commented Apr 19, 2013 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
    Commented Apr 19, 2013 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
    Commented Apr 20, 2013 at 0:22

3 Answers 3

3

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.

1
  • 1
    0 is one of the values considered false when you evaluate it in an if statement. Commented Apr 20, 2013 at 10:54
0

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;
}
0

The issue is to do with this part of update_metadata() function within meta.php that update_post_meta() calls

$result = $wpdb->update( $table, $data, $where );
if ( ! $result )
    return false;

As the return value is 0 then the if statement correctly treats it as false, albeit 0 has been saved to the database.

This only happens when it updates. If a new post meta is added with a value of 0 then it returns the line id as expected.

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.