[wp-trac] [WordPress Trac] #21900: update_post_meta() returns FALSE when meta_value to be updated is the same
WordPress Trac
wp-trac at lists.automattic.com
Sat Sep 15 17:15:18 UTC 2012
#21900: update_post_meta() returns FALSE when meta_value to be updated is the same
-----------------------------+--------------------------
Reporter: mcbenton | Type: defect (bug)
Status: new | Priority: normal
Milestone: Awaiting Review | Component: General
Version: 3.4.2 | Severity: normal
Keywords: |
-----------------------------+--------------------------
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.
--
Ticket URL: <http://core.trac.wordpress.org/ticket/21900>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software
More information about the wp-trac
mailing list