If you are getting a WordPress media attachment page 404 error, the solution may not be obvious. I got very frustrated with the unhelpful answers found searching on Google, so I dug deep to find my own, definitive, solution.
After much trial-and-error checking, it appears that the issue is: the media/image file is attached to a post or page that no longer exists. This is a very subtle error, but removing the nonexistent post reference fixed all of my affected image files!
Fix for WordPress Media Attachment Page 404 Error
In order to see the breakage easily, I made use of a WordPress plugin to show more details about the image file. The plugin was used for visualizing the problem (the attachment to the nonexistent post), and can be used to actually fix the attachment issue. In my case, I removed the attachment completely, but could have re-attached the image to an existing post.
- Setup the Media Library Assistant plugin under the “General” tab, then scroll to “Media/Edit Media Enhancements”.
- Check the box: “Enable Edit Media additional meta boxes” – this adds “Parent Info”, “Menu Order”, “Attachment Metadata” and four “where-used” meta boxes to the Edit Media screen.
- Go to each faulty image file and review the “Parent Info” section – observe NONEXISTENT attachment!
- Fix the attachment as you wish.
Also, go to “Media”, then “Assistant” to observe, under the “Title/Name” column, the status of the media attachment, including UNATTACHED. (This menu item “Assistant” is available with the Media Library Assistant plugin installed.)
How This Happens
Media such as images, uploaded by various plugins, may create a “Page” or “Collection” which you may delete later, but keep the image. In my case I deleted such pages after the fact. The plugins were likely performing as described.