WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have a custom field for a large number of posts that contains the url for a youtube video. I used to grab the ID from the video and use that to build the thumb but in a new version I'm actually using download_url() for the custom built thumb image url from youtube and downloading it to my server. So I have to do this for all posts in my database but need a way to do it other than manually with each one.

So I have a custom field for each post called video_code which equals the url of the video.

So with that I need to do something like this for all posts of a specific post_type..

        require_once(ABSPATH . "wp-admin" . '/includes/image.php');
        require_once(ABSPATH . "wp-admin" . '/includes/file.php');
        require_once(ABSPATH . "wp-admin" . '/includes/media.php');

$video_code = get_post_meta($post->ID,'video_code',true);
$video_id = getVideoId($video_code); // getVideoId is a custom function
if ($video_id) {
     $video_image_url = 'http://img.youtube.com/vi/'.$video_id.'/hqdefault.jpg';
     ut$tmp = download_url($video_image_url);
     $file_array = array(
                'name' => basename( $video_image_url ),
                'tmp_name' => $tmp

$vid_thumb = media_handle_sideload( $file_array, $post_id );

Any ideas?

share|improve this question
up vote 1 down vote accepted

This will fire on init so you just need to refresh a page one time for it to work. I left out the actual value of $new_value -- you need to define that.

add_action( 'init', 'wpse_75308_update_video_meta' );

function wpse_75308_update_video_meta() {
    // Get all posts
    $posts = get_posts( array( 'numberposts' => -1 );

    // Loop through each and update the custom field where it exists
    foreach( $posts as $post ) {
        update_post_meta( $post-ID, 'video_thumb', $new_value );
share|improve this answer
So I refresh the homepage once and this fires? And if the page is refreshed again it refires? – Pollux Khafra Dec 7 '12 at 19:29
Yeah, so firing it over and over will just update the meta again and again. So you just run it once, check that it worked and remove it. – developdaly Dec 7 '12 at 19:30
What about the fact that I'm running media_handle_sideload()? It's just gonna download all those images with the code I have above? Do I need to require anything else? – Pollux Khafra Dec 7 '12 at 19:36
Haven't you already uploaded all of the videos? Why do you need to upload them again? – developdaly Dec 7 '12 at 19:39
I'm not uploading the videos. I'm downloading the video's thumb from the videos url. – Pollux Khafra Dec 7 '12 at 19:41

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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