I’m trying to write a bit of PHP to loop through all my posts and create a thumbnail from a URL containing one of my custom fields.
Most of my posts include a YouTube video and every such post also has a custom field: youtube_id. I can grab the video thumbnail using the Google API with the following URL: http://img.youtube.com/vi/%5Byoutube_id%5D/0.jpg. I then want to import the image and set it as the post’s featured image.
What I was thinking/hoping is that I’d be able to write a script to loop through all of my posts and do something like:
$thumb_url = 'http://img.youtube.com/vi/' . get_post_meta($post->ID, 'youtube_id', true) . '/0.jpg'; set_post_thumbnail($thumb_url);
My theoretical code would use this imagined function, ‘set_post_thumbnail’, which unfortunately doesn’t seem to exist! 🙂 But I’m very new to this sort of thing so my code probably wouldn’t work at all anyway, even if the function did exist!
Can anyone suggest a way to achieve what I’m after (assuming you can make sense of it)? The alternative would be to make a change to my theme to just display the image straight from the URL but I’d rather actually import it so that WordPress crops and resizes it appropriately.
Thanks in advance for your help.
- The topic ‘Function to set the featured image/thumbnail?’ is closed to new replies.