WordPress should I attach or detach images?

There is no downside to having unattached media posts.

This is a long running issue in the wordpress sphere. You may not understand the difference between “unattached” and “unused” in WordPress. WordPress notions of “attachments” and “attached to” status are very, very old, going back to WP 2.0. In some ways attachments are the original custom post type. Because of the way WordPress has evolved in recent years “unattached” is unreliable in many sites and applications. It has a very precise definition; there is a “post_parent” field in the database and if this field contains zero the item is unattached. However, even WordPress itself has flaws in maintaining this field, to say nothing of plugins and themes. Also for cases like “Featured Image” where the same item can be used in multiple posts/pages, what’s the proper “parent”? The original purpose of attaching an item to a post or page was tied to the shortcode (also very old). When WordPress 3.5 changed the way image galleries were composed the notion of “parent” fell into disuse.

The assignment of objects (product, post, page, etc.) to a file in the Media Library is unfortunately very incomplete for historical reasons. You can only assign a maximum of one object to a file in the database. If you now use the image in several places (e.g. on the post and product), WordPress doesn’t know about it. For this reason, many plugins that are used to assign files do not even save this assignment. It just doesn’t make much sense for them.

Deleting all unassigned files now leads to exactly what you have experienced. You have deleted a file that was actually used somewhere. But WordPress didn’nt know about it. As a result, it is gone there. Only using the backup can help to fix this. As far as I know, the revision of the media library is part of the plan for the Gutenberg Editor. I don’t know the schedule for this, but I hope that something will be changed in this context.

WordPress is really bad in keeping media<=>content relationships. WordPress doesn’t monitor where the media file is actually in use.

A bit of background to make sure we are in sync:

  • Uploading an image means that you create a new post (of type attachment) in WordPress database and you create a few image files in WordPress file system (one file per size)
  • Attaching to a post means that you mark the (hereabove) attachment post as child of this post. Meaning that an attachment can be only attached to one post.
  • Inserting an image in a post only means you add an img element to the post. The source (attribute src) points to the url of the file created when you uploaded the image.

WordPress quirks:

  • There is no built-in way to know which images are inserted. You have to parse the content of the post.
  • You cannot undo the parent/child relationship between the post and the attached image unless you delete the post or the image

Quick and dirty: you could look for pattern ‘wp-image-xxx’ where xxx is the post id of the attachment.

<img fetchpriority=”high” decoding=”async” width=”259″ height=”383″ src=”https://website.com/wp-content/uploads/image.jpg” alt=”” class=”wp-image-56″>