Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 1 down vote accepted

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?.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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