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
?
1 Answer
Reset to default 0As 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 totrue
orfalse
. - Utilize some other post meta data type like
string
orinteger
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
版权声明:本文标题:Strict comparisons problem when using boolean post meta 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741534365a2383944.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
if ($value)
works correctly? – Rup Commented May 24, 2021 at 15:12get_post_meta
might not be querying the database here as those values get cached in the object cache, otherwise it is documented that on failureget_post_meta
will return afalse
value – Tom J Nowell ♦ Commented May 24, 2021 at 16:03echo ( 'foo' === 'bar' )
will output''
, you can'techo
the output to get the type,echo false
does not printfalse
, you need a more specific test, such asvar_dump
, e.g.var_dump( false );
which printsbool(false)
– Tom J Nowell ♦ Commented May 24, 2021 at 16:08if ($value)
like it complains about usingif (false == $value)
, and it produces the desired result. – Meyer Auslander Commented May 24, 2021 at 16:14true
is saved as1
, whereasfalse
is saved as an empty string (''
) in the database? Soget_post_meta()
did return the correct values because in the database, we could have exactlyNULL
(and not the string"NULL"
) as the value, but notTRUE
orFALSE
. – Sally CJ Commented May 24, 2021 at 20:25