WordPress.org

Make WordPress Core

Opened 3 months ago

Closed 9 days ago

#47782 closed defect (bug) (invalid)

REST API Media returning blank

Reported by: CHEWX Owned by:
Milestone: Priority: normal
Severity: normal Version: 5.2.2
Component: Media Keywords:
Focuses: rest-api Cc:
PR Number:

Description

Hi,

Just noticed an issue where an image returns empty.

If you go to a post, upload an image. Then delete that post.

The image will still be in the media lib which is correct.

If you then https://wordpress.com/wp-json/wp/v2/media?include=123 it will return empty.

If you restore post that was deleted the image will return.

I would expect the image to return regardless of the post it was uploaded to as if the post was deleted, that image may be re-added within another post, as you wouldn't re-upload as it's already in the media lib.

Thanks,
Tom

Change History (3)

#1 @Otto42
3 months ago

  • Focuses javascript removed
  • Severity changed from blocker to normal

An attachment's post_status is "inherit", meaning that it inherits the status of its parent post.

This code over in class-wp-posts-rest-controller.php checks the status of the parent to determine if the post's parent is readable:

		// Can we read the parent if we're inheriting?
		if ( 'inherit' === $post->post_status && $post->post_parent > 0 ) {
			$parent = get_post( $post->post_parent );
			if ( $parent ) {
				return $this->check_read_permission( $parent );
			}
		}

If the parent post is trashed, then it is not readable, which makes the attachment item also not readable.

#2 @SergeyBiryukov
3 months ago

  • Component changed from REST API to Media
  • Keywords close added

Hi @CHEWX, welcome to WordPress Trac! Thanks for the report.

Yes, the current behavior is intentional, per comment:1.

If you upload an image from the Edit Post screen, then it becomes attached to that post and depends on its status.

If you upload an image from the Media Library screen, then it does not depend on any post.

Last edited 3 months ago by SergeyBiryukov (previous) (diff)

#3 @desrosj
9 days ago

  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Closing out since there has been no further reporter feedback and this is the intended behavior.

@CHEWX if you feel this is incorrect, feel free to reopen with clarification.

Note: See TracTickets for help on using tickets.