How can I insert a post into wordpress and associate it with a category?


I have content from elsewhere that I would like to insert as a post in Wordpress and then associate with an existing category. It is pretty straightforward to insert a new post into the wp_posts table, but I can't seem to figure out how to construct a query to both insert a new post and at the same time associate the post with a category. How would one go about doing this?

If it helps, I am using WordPress 2.8

7/23/2009 3:22:30 PM

Accepted Answer

Use this query:

INSERT INTO wp_posts (post_title,post_content,post_name,post_date,post_date_gmt,post_modified,post_modified_gmt,post_author,post_status) 
VALUES ('title','text','post_name',now(),now(),now(),now(),1,'publish')

INSERT INTO wp_term_relationships (object_id,term_taxonomy_id) VALUES ([the_id_of_above_post],1)
8/5/2015 7:20:49 PM

use the wp_insert_post function then use the wp_set_post_categories function for usage information

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow