wp_set_post_categories()
Set categories for a post.
If no categories are provided, the default category is used.
Uses: wp_set_post_terms()
Hooks from the function
Return
Array|false|WP_Error
. Array of term taxonomy IDs of affected categories. WP_Error or false on failure.
Usage
wp_set_post_categories( $post_id, $post_categories, $append );
- $post_id(int)
- The Post ID. Does not default to the ID of the global $post.
- $post_categories(int[]|int)
- List of category IDs, or the ID of a single category.
Default: empty array - $append(true|false)
- If true, don't delete existing categories, just add on. If false, replace the categories with the new categories.
Default: false
Examples
#1 Move the post programmatically from one category to another:
Note that if we don't set the third parameter, it defaults to false, which means the categories will be replaced rather than added.
wp_set_post_categories( $post_id, [ 123 ] );
Changelog
Since 2.1.0 | Introduced. |