那一句话:
“如果提交的值与数据库中已有的值相同,则返回false。”。。。非常重要(感谢艾哈迈德·福阿德),因为这完全违反直觉!当操作不是真正的失败时,您可以合理地期望数据库更新返回success(true)。但在这里,当数据保持不变时,Wordpress给出了失败(false)。这让我完全被愚弄了,因为除了投稿的笔记外,抄本中没有提到它!
您不认为“失败”意味着无法访问数据库吗?
因此,我使用了一个简短的函数来总结问题:
function check_update_post_meta( $postid, $key, $value ) {
// Get the single/first value of this key in this post\'s meta
$response = get_post_meta( $postid, $key, true );
if ($response == $value) {
// If the value is already set return true
return true;
} else {
// Replace the old value with the new, or add a key into the db
$response = update_post_meta( $postid, $key, $value );
}
return $response;
// Now \'false\' means a failure to reach the database
// Now \'true\' means the data now exists in the database without or with changes
// A return int>0 means a new record with this ID was added
// Note: It\'s not possible that the ID=1 when calling this function from within a post.
}