WordPress.org

Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#7018 closed defect (bug) (fixed)

update_post_meta should add nonexistent values

Reported by: filosofo Owned by:
Milestone: 2.6 Priority: normal
Severity: normal Version: 2.5
Component: General Keywords: update_post_meta
Focuses: Cc:

Description

Currently, if you try to use update_post_meta to update a post meta key and value that don't exist, it just returns false. This behavior is at odds with that of other update functions such as update_option, update_user_option, update_usermeta, wp_update_user, wp_update_category, and update_recently_edited, all of which will create the updated object, if it doesn't already exist.

The other updating functions, which like update_post_meta fail when the object doesn't already exist, would not typically be called when one is agnostic about the existence of the object in question: wp_update_comment and update_attached_file, e.g.; wp_update_term seems to be a possible exception.

Because update_post_meta is semantically similar to update_option and update_usermeta and like them is frequently employed in plugins, it should behave similarly.

Attachments (3)

update_post_meta.diff (2.1 KB) - added by filosofo 6 years ago.
update_post_meta_efficiency.diff (1.5 KB) - added by filosofo 6 years ago.
uploads-meta.patch (938 bytes) - added by azaozz 6 years ago.

Download all attachments as: .zip

Change History (11)

filosofo6 years ago

comment:1 ryan6 years ago

  • Resolution set to fixed
  • Status changed from new to closed

(In [7978]) Add non-existent values instead of returning g false in update_post_meta(). Props filosofo. fixes #7018

comment:2 mdawaffe6 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

update_meta() is now double escaping the meta_value.

$wpdb->update( does escaping for you; you don't need to call $wpdb->escape() first.

comment:3 ryan6 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [7980]) Don't double escape. fixes #7018

comment:4 filosofo6 years ago

I've added a patch that removes now-unnecessary checks for the existence of post meta data.

comment:5 lloydbudd6 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened
  • Version set to 2.5

comment:6 ryan6 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [7985]) Remove unnecessary checks for the existence of post meta data. Props filosofo. fixes #7018

comment:7 azaozz6 years ago

  • Resolution fixed deleted
  • Status changed from closed to reopened

Using update_post_meta() instead of add_post_meta() converts the array of attachment properties to a string when uploading new image. That results in php warnings on the Media Library page.

The patch slightly optimizes update_post_meta() and ensures it calls add_post_meta() when the key doesn't exist.

azaozz6 years ago

comment:8 ryan6 years ago

  • Resolution set to fixed
  • Status changed from reopened to closed

(In [7995]) Fix serialization when adding meta via update_post_meta(). Props azaozz. fixes #7018

Note: See TracTickets for help on using tickets.