Make WordPress Core

Opened 7 years ago

Closed 4 years ago

#21900 closed defect (bug) (wontfix)

update_post_meta() returns FALSE when meta_value to be updated is the same

Reported by: mcbenton Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.4
Component: Options, Meta APIs Keywords:
Focuses: Cc:
PR Number:


Since [14564] update_metadata() (wp-includes/meta.php [20435]) in lines 129-136 checks to see if the meta_value to be updated is the same as the value that already exists in the database. The point of this check, presumably, is to prevent running a needless UPDATE query. That's fine and good.

Currently, if the values are the same the function returns FALSE. I think it should return TRUE in this case. While I understand that from one point of view it should be false, since nothing was actually updated, if I'm trying to update my metadata to a certain value, and the end result is that the value in the database matches the value I submitted, isn't that a win? In other words, even though no UPDATE query was actually executed, the postmeta was "updated" to have the value that was specified in update_post_meta().

Here's an example of when this could be an issue:

function my_plugin_ajax_save_metadata() {
    // create a success flag
    $success = true;
    // $postid passed in via post variables
    $postid = $_POST[ 'postid' ];
    // data fields from post passed in from a serialized array
    parse_str( $_POST[ 'data' ], $fields );
    // my custom post meta fields all have names that begin with _mymeta
    foreach( $fields as $key => $val ) {
        // if a field is one of my meta fields, attempt to update it
        if ( false !== strpos( $key, '_mymeta' ) ) {
            // if $val is the same as what's already in the db, $success will be false
            $success = update_post_meta( $postid, $key, $val );
        if ( false === $success ) {
            // uh-oh, update_post_meta failed, need to handle it
            // but updating a meta value to the same value is not really a fail
            $message = "Error updating metadata: $key = $val";
    echo ( $success ) ? 'Success.' : $message;
add_action( 'wp_ajax_save_meta', 'my_plugin_ajax_save_metadata' );

Of course, I could use get_post_meta() to find out if values have changed before calling update_post_meta(), but since update_metadata() already does this (line 131), it seems wasteful for me to have to do it.

I guess another alternative would be to return something besides TRUE or FALSE when the values are the same, but I'm not sure what that would be.

Change History (5)

#1 @nacin
7 years ago

I think changing this would break compatibility.

It is worth pointing out that this is consistent with the behavior in update_option(), as well.

#2 @knutsp
7 years ago

  • Cc knut@… added

These update_* functions could return a non-boolean value that evaluates to false, like integer 0, in case the option value remains the same. This would probably give fewer compatibility problems.

#3 @nacin
6 years ago

  • Component changed from General to Options and Meta

#4 @knutsp
4 years ago

  • Keywords close added
  • Version changed from 3.4.2 to 3.4

Suggest wontfix.

#5 @MikeHansenMe
4 years ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed
Note: See TracTickets for help on using tickets.