[wp-hackers] Downloading with media_sideload_image

Andrew Nacin wp at andrewnacin.com
Thu Feb 18 04:41:02 UTC 2010

Since you know the post ID, you can fetch that post's attachments, ordered
by post_date descending, and grab the attachment ID that way. Example:

$upload = media_sideload_image( $url, $post_id );
if ( is_wp_error( $upload ) )
die( 'Nope' );
$attachments = get_posts( array(
'post_type' => 'attachment',
 'number_posts' => 1,
'post_status' => null,
'post_parent' => $post_id,
 'orderby' => 'post_date',
'order' => 'DESC',
) );
$thumbnail_id = $attachments[0]->ID;

On Wed, Feb 17, 2010 at 8:22 PM, Alex Dunae <alex at dunae.ca> wrote:

> I'm create a theme that will use YouTube thumbnails as the post thumbnail
> when a YouTube link is present, but I've run into a block with
> media_sideload_image.  It returns an HTML string, not the new attachment's
> ID.  Is there a way to attach a remote image to a post and get its ID as the
> return value?

More information about the wp-hackers mailing list