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.
One Response
how to Show Related Posts for Custom Post Types and Taxonomies???
Trackbacks: