HTTP/2 301
server: nginx
date: Sat, 27 Dec 2025 01:31:47 GMT
content-type: text/html; charset=UTF-8
x-olaf: ⛄
vary: accept, content-type
x-redirect-by: WordPress
location: https://developer.wordpress.org/reference/functions/wp_create_categories/
x-frame-options: SAMEORIGIN
alt-svc: h3=":443"; ma=86400
x-nc: MISS ord 2
HTTP/2 200
server: nginx
date: Sat, 27 Dec 2025 01:31:48 GMT
content-type: text/html; charset=UTF-8
vary: Accept-Encoding
x-olaf: ⛄
vary: accept, content-type
link:
; rel="https://api.w.org/"
link:
; rel="alternate"; title="JSON"; type="application/json"
link: ; rel=shortlink
x-frame-options: SAMEORIGIN
content-encoding: gzip
alt-svc: h3=":443"; ma=86400
x-nc: MISS ord 2
wp_create_categories() – Function | Developer.WordPress.org
wp_create_categories( string[] $categories, int $post_id = '' ): int[]
Creates categories for the given post.
$categoriesstring[]requiredArray of category names to create.
$post_idintoptionalThe post ID.
Default:''
int[] Array of IDs of categories assigned to the given post.
function wp_create_categories( $categories, $post_id = '' ) {
$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;
}
View all references View on Trac View on GitHub
| Version | Description |
|---|
| 2.0.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.