wp_set_post_categories()
Sets categories for a post.
If the categories parameter is not set, the post will be attached to the default category.
When using this function, all old categories of the post will be removed and the specified ones will be set.
Uses: wp_set_post_terms()
Hooks from the function
Returns
Array|false|WP_Error. identifiers - an array of term IDs that were added/attached to the post or a WP_Error object.
Usage
<?php wp_set_post_categories( $post_ID, $post_categories, $append ) ?>
- $post_ID(int)
- ID of the post to be attached to the categories.
- $post_categories(array)
- List of category IDs to which the post should be attached.
Default: array() - $append(bool)
- true - add to existing categories. false - completely update categories (remove from existing and add to specified).
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. |