Make WordPress Core

Opened 3 months ago

Last modified 3 months ago

#47782 new defect (bug)

REST API Media returning blank

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



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.


Change History (2)

#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)
Note: See TracTickets for help on using tickets.