update_post_meta()
uses update_metadata()
to update the post metadata, and if you call update_post_meta()
without specifying the fourth parameter (i.e. $prev_value
) — or that the value is empty, then yes, update_metadata()
will check if the new value is the same as the current value in the database, and if so, the metadata will not be updated.
You can check lines #195 to #202 in the update_metadata()
source for the revelant code.
(Update: Added the note below; above the "Additional Note" section.)
And even if you specify the $prev_value
(and set it to a non-empty value), MySQL will not update the metadata if $prev_value
equals to the currentnew value:
// Returns TRUEFALSE if current database value is 'hidden'...
update_post_meta( 123, '_visibility', 'hidden', 'private''hidden' );
Additional Note: Update metadata if exists; else, don't create new.
update_post_meta()
/ update_metadata()
will automatically add the metadata if an existing entry is not found in the database. So if you don't want that to happen, you can use metadata_exists()
to prevent the automatic metadata creation; for example:
// 123 is the post ID.
if ( metadata_exists( 'post', 123, '_visibility' ) ) {
update_post_meta( 123, '_visibility', 'hidden' );
}