humanmade / network-media-library

Network Media Library plugin for WordPress Multisite
MIT License
288 stars 55 forks source link

Featured image is not saved if the image ID is that of a deleted post ID on the non-media site #49

Open stuajnht opened 5 years ago

stuajnht commented 5 years ago

Expected Behaviour

Using the Gutenberg editor, when setting or updating a featured image, it should be saved with the post.

Current Behaviour

Occasionally the featured image does not get saved with the post. This depends on if the value of the post ID of the image on the media site exists in the non-media site that the featured image is being applied to (for example, a post on the non-media site has been deleted).

This does not happen if the classic editor plugin is being used - the featured image is saved correctly.

Steps to Reproduce

  1. Create a fresh WordPress site, enable multisite and install this plugin (and create the media site)
  2. Create a new page or post on the non-media site and publish it (it should have an ID of 3)
  3. Add at least 3 items to the media gallery
  4. Completely delete the post / page just created (so that it's not just in 'trash')
  5. Create a new post / page and set the featured image to be the third (or later) image uploaded
  6. Press the publish button
  7. The featured image will disappear from the Gutenberg editor
  8. Set the featured image to be the first or second image uploaded
  9. Press the update button - the featured image will remain visible
  10. Set the featured image back to the third (or later) image uploaded
  11. Press the update button - the featured image will change back to the previously selected image

Detailed Description

When a featured image is added from the Gutenberg editor, and the publish / update button is pressed, the editor fires off a REST request to /wp-json/wp/v2/posts/<id>, with a request payload containing featured_media with the ID of the featured image.

The WordPress REST controller passes this request to the WordPress REST post controller. The featured image is passed to the handle_featured_media() function. This function calls the set_post_thumbnail() function. The set_post_thumbnail() function attemps to get the ID of the featured image, but as it doesn't exist on the non-media site set_post_thumbnail() returns false, which then causes handle_featured_media() to return a WP_Error. As the ID of the featured image has not been updated in the posts meta _thumbnail_id, it either doesn't exist or still points to the previous ID.

Additional Information

Wordpress 5.1 Network Media Library 1.4.1 This may or may not be related to issues #11 and #45

InkingHubris commented 5 years ago

Second this behavior. Noticed all featured images in multisite configuration disappeared after install. Using any theme (currently using Astra) even with Blog Designer plugin enabled or disabled.

The only current fix is to install Classic Editor as OP stated.

Wordpress 5.2.2 Network Media Library 1.5.0

adampmoss commented 3 years ago

I have also encountered this issue.

The problem is that when trying to set the thumbnail it first does a check to get_post() in web/wp/wp-includes/post.php on line 7143:

if ( $post && $thumbnail_id && get_post( $thumbnail_id ) ) {

This returns false because the media site's thumbnail ID doesn't exist in the network site. I'm gonna have a look at a workaround today but unfortunately hooks are light in this area.

Wordpress: 5.5 Network Media Library: 1.5.0