0

I need to add a post content via database.. I added a post_title, post_description, categories and tags.. I have tried featured image to set via database but its not working.. Can anyone suggest me to set featured image via database.

In wp_posts Table,

INSERT INTO `wp_posts`(`post_type`, `guid`, `post_status` , `post_mime_type`) VALUES ('attachment', 'http://localhost/demo/wp-content/uploads/2016/11/science-2.jpg', 'publish', 'image/jpeg')

In wp_postsmeta Table,

INSERT INTO `wp_postmeta`( `post_id`, `meta_key`, `meta_value`) VALUES (12 , '_thumbnail_id', 20)

Its showing like this empty image..

enter image description here

Suggest me what to do for that....

1 Answer 1

0

First of all, never ever use Plain SQL query to WordPress, because it's too dangerous. Try $wpdb class, if you need to.

BTW, adding posts, post description, assigning featured image, etc. there are wrapper functions designated for these particular tasks, like:

Always, try to consult Codex or Developer Guide for true direction.

4
  • I read those links.. These are based on Php Coding. But i Need to add featured image in database itself.. Can You say how to add featured image using database query..
    – Preethi
    Nov 2, 2016 at 6:45
  • I don't understand your SQL to PHP way. Anyway, your INSERT INTO 'wp_postmeta'( 'post_id', 'meta_key', 'meta_value') VALUES ($parent_post_id , '_thumbnail_id', $image_id) should work. Nov 2, 2016 at 7:29
  • And in the first query, post_status should be inherit Nov 2, 2016 at 7:34
  • I changed post_status from publish to inherit, but still its looking like that screenshot..
    – Preethi
    Nov 2, 2016 at 8:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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