Home » Functions » set_post_thumbnail

The set_post_thumbnail function in WordPress is used to set a featured image, also known as a post thumbnail, for a specific post. The featured image is a visual representation of the post that can be displayed on the website’s front-end, such as in a blog post preview or on a homepage.

By using set_post_thumbnail, you can easily assign an image as the featured image for a specific post. This function takes two parameters: the post ID and the attachment ID of the image you want to set as the featured image. The post ID identifies the post you want to assign the image to, while the attachment ID represents the image itself.

Here’s an example usage code:

// Set the featured image for post with ID 123
$attachment_id = 456; // ID of the image attachment
set_post_thumbnail(123, $attachment_id);

In this example, we are setting the featured image for a post with the ID 123. The attachment ID 456 represents the image that we want to assign as the featured image. After calling the set_post_thumbnail function, the image will be associated with the post as its featured image.

It’s important to note that the set_post_thumbnail function should be used within the WordPress loop or after the post has been saved. This ensures that the function has access to the necessary post data and can properly set the featured image.

So, if you’re looking to enhance the visual appeal of your WordPress posts, the set_post_thumbnail function is a handy tool to help you easily assign and display featured images.

Learn More on

Register an account to save your snippets or go Pro to get more features.