WordPress.org

Make WordPress Core

Opened 9 years ago

Closed 6 years ago

Last modified 19 months 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:

Description

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";
            break;
        }
    }
    echo ( $success ) ? 'Success.' : $message;
    exit;
}
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 (6)

#1 @nacin
9 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
9 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
8 years ago

  • Component changed from General to Options and Meta

#4 @knutsp
6 years ago

  • Keywords close added
  • Version changed from 3.4.2 to 3.4

Suggest wontfix.

#5 @MikeHansenMe
6 years ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to wontfix
  • Status changed from new to closed

#6 @konradg2
19 months ago

So you think it's ok to not distinguish connection/query error from situation when record is not changed because it's the same as in database?
These are two very different cases and application's reactions will most likely be very different to each one.

If BC is the only reason, why don't you provide alernative for update_metadata which behaves reasonably?

Last edited 19 months ago by konradg2 (previous) (diff)
Note: See TracTickets for help on using tickets.