[wp-trac] [WordPress Trac] #58763: Inconsistent add/get/update/delete_post_meta() functions leads to deleting post metadata.

WordPress Trac noreply at wordpress.org
Mon Oct 9 19:54:31 UTC 2023


#58763: Inconsistent add/get/update/delete_post_meta() functions leads to deleting
post metadata.
-------------------------------------------------+-------------------------
 Reporter:  jsmoriss                             |       Owner:  (none)
     Type:  defect (bug)                         |      Status:  new
 Priority:  normal                               |   Milestone:  6.4
Component:  General                              |     Version:
 Severity:  major                                |  Resolution:
 Keywords:  has-patch needs-testing needs-       |     Focuses:
  testing-info                                   |
-------------------------------------------------+-------------------------

Comment (by jsmoriss):

 Replying to [comment:5 nicolefurlan]:
 > This ticket could use some testing info to make things easier for
 testers. @jsmoriss would you be able to add them?

 Off the top of my head, here's some code to define a post metadata key for
 the post (not the revision), **then when a revision is restored**, the
 post metadata is erased because get_post_meta() gets the revision metadata
 (which does not exist) and update_post_meta() updates the post metadata.

 {{{
 add_action( 'save_post', 'test_save_post', 10, 1 );

 function test_save_options( $post_id ) {

         $post_type = get_post_type( $post_id );

         if ( 'revision' !== $post_type ) {

                 add_post_meta( $post_id, '_test_metadata', 'OK' );      //
 Adds POST metadata once.

                 $val = get_post_meta( $post_id, '_test_metadata', $single
 = true );     // Value should always be "OK".

                 error_log( $post_id . ' ' . $post_type . ' _test_metadata
 = ' . $val . ' (value should always be "OK" )' );

                 if ( empty( $val ) ) {

                         error_log( $post_id . ' ' . $post_type . '
 resetting test (by deleting _test_metadata)' );

                         delete_post_meta( $post_id, '_test_metadata' );
                 }

         } else {

                 $val = get_post_meta( $post_id, '_test_metadata', $single
 = true ); // Retrieves REVISION metadata (which does not exist).

                 update_post_meta( $post_id, '_test_metadata', $val ); //
 Updates the POST metadata (not the revision metadata).

                 error_log( $post_id . ' ' . $post_type . ' _test_metadata
 = ' . $val . ' (post metadata value now erased)' );
         }
 }
 }}}

 The error log output looks something like this:

 {{{
 [09-Oct-2023 19:44:46 UTC] 1086 page _test_metadata = OK (value should
 always be "OK" )
 [09-Oct-2023 19:45:27 UTC] 4624 revision _test_metadata =  (post metadata
 value now erased)
 [09-Oct-2023 19:45:27 UTC] 1086 page _test_metadata =  (value should
 always be "OK" )
 [09-Oct-2023 19:45:27 UTC] 1086 page resetting test (by deleting
 _test_metadata)
 }}}

 js.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/58763#comment:6>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list