Avoid PHP notices in media modal caused by orphaned attachments.

Merges [29724] to the 4.0 branch.

props SergeyBiryukov.
fixes #29566.

Built from https://develop.svn.wordpress.org/branches/4.0@30259


git-svn-id: http://core.svn.wordpress.org/branches/4.0@30259 1a063a9b-81f0-0310-95a4-ce76da25c4cd