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.

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

2 Answers 2

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);
wp_update_post($post);
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 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

 
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.