[wp-trac] [WordPress Trac] #52787: Empty array for non-single post meta breaks post save through REST API

WordPress Trac noreply at wordpress.org
Fri Mar 12 09:36:01 UTC 2021


#52787: Empty array for non-single post meta breaks post save through REST API
--------------------------+-----------------------------
 Reporter:  BrechtVds     |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  REST API      |    Version:  5.7
 Severity:  normal        |   Keywords:
  Focuses:  rest-api      |
--------------------------+-----------------------------
 The change in #50790 causes an "Updating failed. Could not delete meta
 value from database." error when saving post meta in Gutenberg.

 The reason seems to be that Gutenberg uses default values for any post
 meta that has been registered for the REST API. For non-single meta that's
 an empty array. Now when someone makes a meta change and tries to save it
 in Gutenberg, that empty array gets passed along. Because of the change,
 the WP_REST_Meta_Fields::update_value() sees an empty array and wants to
 delete that non-existing meta, causing the error.

 Can be replicated by adding this sample plugin file:

 {{{
 <?php
 /*
 Plugin Name: Test Bug Plugin
 Description: Testing bug.
 Version: 1.0
 License: GPLv2 or later
 */

 function test_bug_plugin_register_meta() {
     register_meta( 'post', 'example-post-meta-field', array(
                 'show_in_rest' => true,
                 'single' => false, // Is the default value. Single = true
 doesn't cause the problem.
         ) );
 }
 add_action( 'init', 'test_bug_plugin_register_meta');
 }}}

 Then use a simple plugin like [https://wordpress.org/plugins
 /conditionally-display-featured-image-on-singular-pages/] which adds a
 checkbox in the "Featured Image" section when creating a post, which gets
 saved as post meta.

 This video shows the error message:
 https://www.loom.com/share/025fe7b6f2f44955b303daf3317f88d0

 Wasn't sure if this should be logged as a Gutenberg or core bug. But
 Gutenberg defaulting to an empty array for non-single meta seems logical
 to me, so I feel like this should get fixed in the REST API.

 First Trac ticket. Let me know if I need to add/change anything!

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


More information about the wp-trac mailing list