How to add/assign or change featured image in post programmatically?

Try using set_post_thumbnail().

Assuming you already know how to determine the $post (ID or object) for which to set the featured image, and the $thumbnail_id (ID) of the image that you want to set as the featured image:

set_post_thumbnail( $post, $thumbnail_id );

Leave a Comment