I saw this code as how to set a featured image for a post:

// required libraries for media_sideload_image
require_once(ABSPATH . 'wp-admin/includes/file.php');
require_once(ABSPATH . 'wp-admin/includes/media.php');
require_once(ABSPATH . 'wp-admin/includes/image.php');

// $post_id == the post you want the image to be attached to
// $video_thumb_url == the vimeo video's thumb url
// $description == optional description

// load the image
$result = media_sideload_image($video_thumb_url, $description);

// then find the last image added to the post attachments
$attachments = get_posts(array('numberposts' => '1', 'post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => 'ASC'));

if(sizeof($attachments) > 0){
    // set image as the post thumbnail
    set_post_thumbnail($post_id, $attachments[0]->ID);

But how do I get that information when I retrieve a post? Is there a property or method that I use?

  • Do you wanted get post featured image url? – WPTC-Troop Jan 7 '16 at 19:19

 [0] => url
 [1] => width
 [2] => height
 [3] => boolean: true if $url is a resized image, false if it is the original or if no image is available.

In the above example you can get the src and id using:

$image_src  = media_sideload_image($video_thumb_url, $description, 'src');
$image_id   = attachment_url_to_postid($image_src); 
set_post_thumbnail($post_id, $image_id );
$url = wp_get_attachment_url(get_post_thumbnail_id(get_the_ID()));

above code get current featured image url. I hope it work for you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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