I am trying to set thumbnails for my post.

I have post_id = 285 and I want to add manually a thumbnail (using code) but I'm not succeeding.

update_post_meta( $post->ID, '_thumbnail_id', $attachment_id );

I got this function reference but I do not understand what is the $attachment_id and how can I get it.

I have also image name and path of image.


First you'll need to upload the image, like this. This will upload the image and add a row to wp_posts where your attachment is.

How dynamic do you need this to be? Can you upload the image with the user interface or does that have to be done programatically as well?

In your case, the third parameter of update_post_meta() should be the ID of the image in the wp_posts table.

Edit: This is more what you need regarding the image upload: How to add image to post programmatically?.

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.