There often time when you want to add categories or tags to a post from code. Wordpress codex has a good deal of resource on this but most of the time it's hard for the beginners to grasp. In this post we will try to crack this the easiest way.
First let us create an array of categories id. Then we simply have to use the wp_set_object() function, which by default take 3 parameters:
Here is how tags can be added:
Related Posts:
Inserting Posts Programatically to WordPress
Insert Custom Content After Each Post
First let us create an array of categories id. Then we simply have to use the wp_set_object() function, which by default take 3 parameters:
- The post id.
- Array of categories to add to the post.
- taxonomy type (category in this example).
$category_ids = array(4, 5, 6);
wp_set_object_terms( $post_id, $category_ids, 'category');
Here is how tags can be added:
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".
Related Posts:
Inserting Posts Programatically to WordPress
Insert Custom Content After Each Post
Comments
Post a Comment