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
Use this query:
Categories are stored in the
wp_terms
tables, with a cross-reference betweenwp_posts
andwp_terms
stored in thewp_term_relationships table
.So, you would first need to insert your post into the
wp_posts
table, and then for each of the existing categories that you want to associate it with, insert a record into thewp_term_relationships
table.More info here: WordPress Database Description
use the wp_insert_post function then use the wp_set_post_categories function
http://codex.wordpress.org/Function_Reference for usage information