Attention Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp set post categories


Set categories for a post.


<?php wp_set_post_categories$post_ID$post_categories$append ?>


(integer) (optional) Post ID.
Default: 0
(array) (optional) Array of category IDs.
Default: array
(boolean) (optional) If true, categories will be appended to the post. If false, categories will replace existing categories.
Default: false

Return Values

Returns an array of category IDs that were assigned to the post ID.



If no categories are passed with a post ID that has a post type of post, the default category will be used.

Be careful, as wp_set_post_categories will overwrite any existing categories already assigned to the post unless $append is set to true.

If an ID is passed with the categories array that is not associated with a valid category, it will be stripped before the object terms are updated and from the return array.

wp_set_object_terms() performs the same function with more granular control for built in categories and can also be used to set any custom taxonomies.

Change Log

Since: 2.1.0

Source File

wp_set_post_categories() is located in wp-includes/post.php.


See also index of Function Reference and index of Template Tags.
This article is marked as in need of editing. You can help Codex by editing it.