Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

💡 Add Postmeta to Each Attachment to Store Array of IDs of Posts Where Attachment is Used #1435

Closed
KTS915 opened this issue May 7, 2024 · 1 comment · Fixed by #1446
Closed

Comments

@KTS915
Copy link
Member

KTS915 commented May 7, 2024

Context

We've started to make the Media Library more usable by (a) creating media categories and tags by default, and (b) enabling storage of media items in folders according to year. There's also a PR to store media files according to media category, though that's incomplete.

There are some more things to do to make the Library as well as it should. At the moment, there's no way to know where a media item is actually used (or if it's used at all). This could be extremely important if we later enable administrators to move the real location of their files. At the moment, moving a file breaks the link from the post to the media file. If, however, we know which posts are affected, we would be able to update such links whenever a media file is moved.

Unfortunately, it would currently require a (very expensive) database query to get the IDs of the posts where a media file is used. Much better would be to store those IDs as an array in the meta for the attachment.

Note that "used" is not the same as "attached". Many files are displayed in the content of posts to which they are not attached.

My thought is that such metadata could be added or updated when any of the following happens:

  • An upload is attached to a post or page (see the add_attachment hook)
  • An upload is inserted into post content (see the wp_media_attachment_action filter)
  • An upload is added or removed to/from a post's content ( see the post_updated hook)
  • An upload is updated (whether via the list or grid view in the Media Library)

Will you be able to help with the implementation?

I might be able to submit a PR, but I wouldn't object if someone else did it before me!

@KTS915 KTS915 added type: feature request New feature or request status: needs triage This issue needs revision, splitting, or other "gardening" work status: discussion Discussion ongoing, no decision made yet. and removed status: needs triage This issue needs revision, splitting, or other "gardening" work labels May 7, 2024
@KTS915
Copy link
Member Author

KTS915 commented May 13, 2024

On further reflection, I think this would be better done using the new object relationships table in PR #1429. That would enable the data to be accessible both from attachments and from posts or pages.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
1 participant