10

Add categories or tags to post programatically

http://feedproxy.google.com

The first thing to do is to create an array of categories id. Once done, you simply have to use the wp_set_object() function, which take 3 parameters: The post id, an array of categories to add to the post, and the taxonomy type (category in this example).

$category_ids = array(4, 5, 6);
wp_set_object_terms( $post_id, $category_ids, 'category');

Adding tags to a post is extremely easy as well. The only difference with the code to add categories is the taxonomy "post_tag" instead of "category".

$tag_ids = array(7, 8, 9);
wp_set_object_terms( $post_id, $tag_ids, 'post_tag');

Thanks to WPProgrammer for this very cool snippet.
Looking for WordPress hosting? Try WP Web Host. Prices starts at $5/month and you can try it for free!Add categories or tags to post programatically

Created by Thomas 1 year 28 weeks ago – Made popular 1 year 28 weeks ago
Category: Wordpress Hacks