[wp-trac] [WordPress Trac] #32224: delete_post_meta() not working as expect when $meta_value is specified as 0 or '0'

WordPress Trac noreply at wordpress.org
Fri May 1 13:53:15 UTC 2015


#32224: delete_post_meta() not working as expect when $meta_value is specified as 0
or '0'
--------------------------------+-----------------------------
 Reporter:  husobj              |      Owner:
     Type:  defect (bug)        |     Status:  new
 Priority:  normal              |  Milestone:  Awaiting Review
Component:  Options, Meta APIs  |    Version:  4.2.1
 Severity:  normal              |   Keywords:
  Focuses:                      |
--------------------------------+-----------------------------
 The
 [delete_post_meta()](https://codex.wordpress.org/Function_Reference/delete_post_meta)
 codex says that if $meta_value is specified this is used to differentiate
 between several fields with the same key. If left blank, all fields with
 the given key will be deleted.

 I have just created an instance where I have multiple numeric meta values
 stored with the same key.
 For example:

 {{{
 $meta = get_post_meta( $post_id, 'my_var' );
 print_r( $meta );
 }}}

 outputs:

 {{{
 Array (
     [0] => 57
     [1] => 103
     [2] => 0
     [3] => 97
     [4] => 4
 )
 }}}

 The following works as expected and deletes the meta value 103:

 {{{
 delete_post_meta( $post_id, 'my_var', 103 );
 }}}

 However, if I try to delete the zero value, it deletes all the meta!

 {{{
 delete_post_meta( $post_id, 'my_var', 0 );
 // or
 delete_post_meta( $post_id, 'my_var', '0' );
 }}}

 Is the ''empty'' check too strict? It should only be deleting all meta if
 the value is an empty string, not a zero value.

--
Ticket URL: <https://core.trac.wordpress.org/ticket/32224>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list