admin管理员组

文章数量:1291565

register_meta(
    'post',
    'my_boolean_meta_value',
    array(
        'show_in_rest' => true,
        'type'         => 'boolean',
        'single'       => true,
        'default'      => true,
    )
);
update_post_meta(166,'my_boolean_meta_value',false);
update_post_meta(167,'my_boolean_meta_value',true);
$false_value=get_post_meta(166,'my_boolean_meta_value',true);
$true_value=get_post_meta(167,'my_boolean_meta_value',true);
if (false === $false_value) {
    echo "Verified false_value is false." . '<br>';
}
if (true === $true_value) {
    echo "Verified true_value is true";
}
if ('' === $false_value) {
    echo "Verified false_value is an empty string. " . '<br>';
}
if ('1' === $true_value) {
    echo "Verified true_value is '1'";
}

The expected output should indicate that $true_value is true, and $false_value is false. Instead the output is:

Verified false_value is an empty string.
Verified true_value is '1'

Can we expect in future versions of WordPress that boolean meta data will always return an empty string instead if false and a string of '1' instead of true?

register_meta(
    'post',
    'my_boolean_meta_value',
    array(
        'show_in_rest' => true,
        'type'         => 'boolean',
        'single'       => true,
        'default'      => true,
    )
);
update_post_meta(166,'my_boolean_meta_value',false);
update_post_meta(167,'my_boolean_meta_value',true);
$false_value=get_post_meta(166,'my_boolean_meta_value',true);
$true_value=get_post_meta(167,'my_boolean_meta_value',true);
if (false === $false_value) {
    echo "Verified false_value is false." . '<br>';
}
if (true === $true_value) {
    echo "Verified true_value is true";
}
if ('' === $false_value) {
    echo "Verified false_value is an empty string. " . '<br>';
}
if ('1' === $true_value) {
    echo "Verified true_value is '1'";
}

The expected output should indicate that $true_value is true, and $false_value is false. Instead the output is:

Verified false_value is an empty string.
Verified true_value is '1'

Can we expect in future versions of WordPress that boolean meta data will always return an empty string instead if false and a string of '1' instead of true?

Share Improve this question asked May 24, 2021 at 15:06 Meyer AuslanderMeyer Auslander 1134 bronze badges 6
  • 1 Does it matter exactly what the value is, as long as if ($value) works correctly? – Rup Commented May 24, 2021 at 15:12
  • if future versions of WP changed the behaviour and returned something different, it would be considered a major backwards compatibility bug. Note that get_post_meta might not be querying the database here as those values get cached in the object cache, otherwise it is documented that on failure get_post_meta will return a false value – Tom J Nowell Commented May 24, 2021 at 16:03
  • Also echo ( 'foo' === 'bar' ) will output '', you can't echo the output to get the type, echo false does not print false, you need a more specific test, such as var_dump, e.g. var_dump( false ); which prints bool(false) – Tom J Nowell Commented May 24, 2021 at 16:08
  • Rup, Good Point. phpcs does not complain about using if ($value) like it complains about using if (false == $value), and it produces the desired result. – Meyer Auslander Commented May 24, 2021 at 16:14
  • @MeyerAuslander, are you aware that the boolean true is saved as 1, whereas false is saved as an empty string ('') in the database? So get_post_meta() did return the correct values because in the database, we could have exactly NULL (and not the string "NULL") as the value, but not TRUE or FALSE. – Sally CJ Commented May 24, 2021 at 20:25
 |  Show 1 more comment

1 Answer 1

Reset to default 0

As stated by Tom, in the comments on the question, it seems not likely that WordPress will change their standard for storing/returning boolean post meta, due to backward compatibility issues. However, I thought of some ways to write code that will work even if the WordPress standard does change their standard:

  • Always convert Boolean Post Meta data to it's exact boolean value using the php boolval function. e.g., boolval( get_post_meta( $id, 'my_boolean_post_meta', true ) );. If so, it will now work to do strict comparisons to true or false.
  • Utilize some other post meta data type like string or integer to implement boolean variables. Choose your own standards for what value will be true and false. Then define them as global constants.

BTW, even though WordPress stores true as '1' and false as '', the following meta queries still work (at least up to version 5.7.2):
For getting true values:

array(
    'key'     => 'my_boolean_post_meta',
    'value'   => true,
    'compare' => '=',
),

For getting false values:

array(
    'key'     => 'my_boolean_post_meta',
    'value'   => false,
    'compare' => '=',
),

It seems 'compare' => '=' does not result in a strict comparison.

本文标签: Strict comparisons problem when using boolean post meta