Last active
April 18, 2017 10:56
-
-
Save robbens/fbc479f4c47af7764ffa2514df18acdd to your computer and use it in GitHub Desktop.
Download and set image as Featured Image in WordPress.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// Download and attach image to post. | |
media_sideload_image($item->image, $post_id, $item->image_caption); | |
// Then find the last image added to the post attachments. | |
$attachment = array_values(get_attached_media( 'image', $post_id ))[0]; | |
// Finally set image as the post thumbnail. | |
if ($attachment) { | |
set_post_thumbnail($post_id, $attachment->ID); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment