0

I am using set_post_thumbnail in order to programmatically attach an image to my posts.

I was wondering how to set a different alt text for this image, for every post, meaning that the same image might appear in 2 or more posts, with a different alt text.

2 Answers 2

1

Got it - update_post_meta has to be used for _wp_attachment_image_alt

1
  • 1
    This will change the alt text for all posts that use that image as its thumbnail, which is not what you wanted, based on your question. Oct 10, 2022 at 10:52
0

You have a global alt (meta) value associated with an attachment(viewable when managed via "media" in the admin) that's stored in the post meta table, but on a post by post basis, the alt value you set when you use that piece of media into a post is stored more literally as a portion post content HTML.

The issue i think is that WordPress doesn't respect the global(main) attachment alt value and will update the attachment meta when you insert it into a post and set the alt text, whilst the specific post alt text (post content html) for that attachment will be whatever you set it to at the time it was inserted.

By calling update_post_meta on _wp_attachment_image_alt all you'll likely to achieve is a short term fix for setting what the current alt value is, it will likely change the next time you insert it and set a differing alt value.

Your Answer

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

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