I’m having trouble to add a featured image to an existing post via PHP. The image resides on wp-content/upload folder. The script i’m doing, is not a plugin, theme, or something like that. It’s just an automation script that runs whenever it’s called.
What is the best approach for this case?
The trick is
media_sideload_image()
andset_post_thumbnail()
.media_sideload_image()
assumes you can grab the URL to the image, whether it exist in/wp-content/
or somewhere else (another site, even). As long as you can programmatically reference the image’s URL, something like this should work.Function Reference/media sideload image « WordPress
Codex
Function Reference/set post thumbnail « WordPress
Codex