0

I understand that an "Attachment" object can have its own metadata in Media Library. However, it can also have it's own metadata in a post scope.

How does WordPress deals with attachment metadata? Does it create a new registry in the database like a "parent-child" relationship for metadata in content?

I couldn't see any reference for it in the source code for wp_get_attachment_metadata() function: https://core.trac.wordpress.org/browser/tags/4.9.8/src/wp-includes/post.php#L5176

Thanks

1 Answer 1

1

Like almost all content in WordPress, a Media item (aka attachment) is stored in the wp_posts table, and its metadata is stored in the wp_postmeta table. The wp_postmeta data is linked to the post by the post ID. If the Media item is set as an attachment to a specific post, then inside the wp_posts table its parent is set to the Post ID of the actual Post. Items uploaded directly to the Media Library are unattached - that is, they have no parent set. In either case, the attachment/Media item itself only has one type of meta - whatever metadata is assigned to it, which can be seen either in the Media Library or in the Insert Media modal.

If you edit media meta (such as the alt text) in either the Media modal while editing a post, or within the Media Library directly, WP saves that to the database in wp_postmeta associated to the media itself and will use that meta whenever you add the media to a new Post/Page/CPT. However, if you edit the alt text or other information within the Editor - just in the post content itself - that only gets saved to the wp_posts table of the post itself - it only affects the HTML within the associated Post. It does not change that attachment's saved metadata, so if you then add the image to a different post, it will use the wp_postmeta alt text from the image and not the updated alt text you used inside a post.

5
  • Thanks! I think you can complement your answer by saying that, when editing metadata from an attachment in Media Library, it saves the changes in the database in wp_postmeta. While changing the metadata from an attachment inside the post (using WordPress content editor), it just changes the alt attribute (etc) in the post's post_content Aug 28, 2018 at 17:53
  • Right. If you change the metadata, such as alt text, in the Media modal itself, it saves that data in wp_postmeta associated to the attachment and will be reusable wherever you add that same image. But if you change anything in the content editor itself, it's only applicable to that one Post, not saved to the attachment's metadata in wp_postmeta.
    – WebElaine
    Aug 28, 2018 at 18:31
  • @LucasBustamante And when you do that the change is made directly in HTML on the image tag in the content. It's not saved in the database in any other way. In that sense you're not actually editing the attachment anymore, you're editing the post content. Aug 29, 2018 at 16:09
  • Thank you both. That was the core of the question. Do you see fit to add it to your answer so I can accept it, @WebElaine? Aug 29, 2018 at 16:11
  • Sure thing, edited above.
    – WebElaine
    Aug 29, 2018 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.