Set, get and remove featured images in code
The featured image of a post is simply the ID of an attachment, stored in the _thumbnail_id meta field. The template functions read that ID and build a complete <img> tag with srcset, size and alt text. Everything else can also be done in code: setting the image during an import, checking whether a post has one, getting only its URL, or downloading an image from another site and making it the featured image.
Problem: An import script or a REST handler must assign featured images to posts, sometimes from a remote URL, and templates need the image in different forms.
Solution: Use has_post_thumbnail(), get_post_thumbnail_id(), get_the_post_thumbnail() and get_the_post_thumbnail_url() to read it, set_post_thumbnail() and delete_post_thumbnail() to change it, and media_sideload_image() to bring in a remote image first.
Reading
<?php
$post_id = 42;
if ( has_post_thumbnail( $post_id ) ) {
$id = get_post_thumbnail_id( $post_id ); // attachment ID
$url = get_the_post_thumbnail_url( $post_id, 'large' ); // only the URL of one size
$img = get_the_post_thumbnail( $post_id, 'medium', array( 'class' => 'card__image' ) ); // the full <img> tag
echo $img;
}
The theme needs add_theme_support( 'post-thumbnails' ), otherwise the “Featured image” box does not appear in the editor.
Setting and removing
<?php set_post_thumbnail( $post_id, $attachment_id ); // any image in the media library delete_post_thumbnail( $post_id ); // removes the link, the image stays in the library
From a remote URL
media_sideload_image() downloads the file, adds it to the media library (attached to the post) and with 'id' as the fourth argument returns the attachment ID:
<?php
function ha_set_featured_image_from_url( $post_id, $image_url ) {
require_once ABSPATH . 'wp-admin/includes/media.php';
require_once ABSPATH . 'wp-admin/includes/file.php';
require_once ABSPATH . 'wp-admin/includes/image.php';
$attachment_id = media_sideload_image( esc_url_raw( $image_url ), $post_id, null, 'id' );
if ( is_wp_error( $attachment_id ) ) {
return $attachment_id;
}
set_post_thumbnail( $post_id, $attachment_id );
return $attachment_id;
}
The three files are loaded only in the admin, so include them in front-end code, cron jobs and WP-CLI commands. The function accepts only URLs that end with an image extension (jpg, png, gif); for image URLs without one, download the file with download_url() and pass it to media_handle_sideload().
Running an import twice should not download the same image twice. Store the source URL in the attachment’s meta and look for it before downloading.
In templates, with a fallback
<?php
if ( has_post_thumbnail() ) {
the_post_thumbnail( 'medium_large', array( 'class' => 'card__image', 'loading' => 'lazy' ) );
} else {
printf(
'<img src="%s" alt="" class="card__image" loading="lazy">',
esc_url( get_template_directory_uri() . '/img/placeholder.jpg' )
);
}
The alt text comes from the attachment (the “Alternative Text” field in the media library). Do not replace it with the post title: the image is usually next to the title, and a screen reader would read it twice. A placeholder image is decorative and gets an empty alt="".
NOTE: media_sideload_image() makes an HTTP request and processes the image, which can take seconds. Never call it during a normal page view; use it in admin actions, cron jobs or WP-CLI, and process large imports in batches.