WordPress.org

Codex

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

Function Reference/wp set post categories

Description

Set categories for a post.

Usage

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

Parameters

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

Return Values

(boolean|mixed)
Returns an array of category IDs that were assigned to the post ID.

Examples

Notes

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.

Related

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.