WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We do a lot of post maintenance outside of the WordPress interface and as a result we need ways of categorising posts. Obviously Wordpress has categories (aka taxonomies) on posts and I plan on using them, but haven't found a way that I can do that in code.

We also use custom post types extensively so the accepted answer may need to state how to incorporate categories into them.

share|improve this question
up vote 0 down vote accepted

There's the wp_update_post(), also.

According to categories, please refer to the documentation.

Categories need to be passed as an array of integers that match the category IDs in the database. This is the case even where only one category is assigned to the post.

So, it would be something like:

$post = get_post($i = $post_id);
$post->post_category = array ($cat1, $cat2, $cat3);
share|improve this answer
This is exactly what I'm looking for, along with get_the_category() to iterate through and get the category ID – Brian Barnes Jan 25 '12 at 23:22
Can you tell me how can I insert a whole category into the specific post. for example: have 4 posts in a category "zanimljivosti" and I want to put that category to be shown inside another post not as a link but to show all 4 inside that new post. Can I do that? – manic Feb 13 '13 at 11:29
For anyone reading this now (2014), you don't seem to be able to use the post_category anymore - you need to use wp_set_post_terms() (eg. stackoverflow.com/questions/8913082/…). – Matty J Mar 28 '14 at 8:16

You should be able to do just about everything you're looking to do between wp_insert_post(), wp_set_object_terms(), and wp_insert_term(). Those all work well with custom post types and custom taxonomies. If you can be a bit more specific about any issues you're encountering (possibly in a new question...depending how far off it is from this one) someone should be able to provide example code.

share|improve this answer

Your Answer


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.