wp_create_categories()
Creates categories for the given post.
No Hooks.
Returns
Int[]. Array of IDs of categories assigned to the given post.
Usage
wp_create_categories( $categories, $post_id );
- $categories(string[]) (required)
- Array of category names to create.
- $post_id(int)
- The post ID.
Default:''
Changelog
| Since 2.0.0 | Introduced. |
wp_create_categories() wp create categories code WP 7.0
function wp_create_categories( $categories, $post_id = 0 ) {
$cat_ids = array();
foreach ( $categories as $category ) {
$id = category_exists( $category );
if ( $id ) {
$cat_ids[] = $id;
} else {
$id = wp_create_category( $category );
if ( $id ) {
$cat_ids[] = $id;
}
}
}
if ( $post_id ) {
wp_set_post_categories( $post_id, $cat_ids );
}
return $cat_ids;
}