Title: wp_create_categories
Published: April 25, 2014
Last modified: February 24, 2026

---

# wp_create_categories( string[] $categories, int $post_id ): int[]

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/wp_create_categories/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_create_categories/?output_format=md#return)
 * [Source](https://developer.wordpress.org/reference/functions/wp_create_categories/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_create_categories/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_create_categories/?output_format=md#changelog)

[ Back to top](https://developer.wordpress.org/reference/functions/wp_create_categories/?output_format=md#wp--skip-link--target)

Creates categories for the given post.

## 󠀁[Parameters](https://developer.wordpress.org/reference/functions/wp_create_categories/?output_format=md#parameters)󠁿

 `$categories`string[]required

Array of category names to create.

`$post_id`intoptional

The post ID. Default empty.

## 󠀁[Return](https://developer.wordpress.org/reference/functions/wp_create_categories/?output_format=md#return)󠁿

 int[] Array of IDs of categories assigned to the given post.

## 󠀁[Source](https://developer.wordpress.org/reference/functions/wp_create_categories/?output_format=md#source)󠁿

    ```php
    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;
    }
    ```

[View all references](https://developer.wordpress.org/reference/files/wp-admin/includes/taxonomy.php/)
[View on Trac](https://core.trac.wordpress.org/browser/tags/6.9.4/src/wp-admin/includes/taxonomy.php#L78)
[View on GitHub](https://github.com/WordPress/wordpress-develop/blob/6.9.4/src/wp-admin/includes/taxonomy.php#L78-L97)

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_create_categories/?output_format=md#related)󠁿

| Uses | Description | 
| [category_exists()](https://developer.wordpress.org/reference/functions/category_exists/)`wp-admin/includes/taxonomy.php` |

Checks whether a category exists.

  | 
| [wp_create_category()](https://developer.wordpress.org/reference/functions/wp_create_category/)`wp-admin/includes/taxonomy.php` |

Adds a new category to the database if it does not already exist.

  | 
| [wp_set_post_categories()](https://developer.wordpress.org/reference/functions/wp_set_post_categories/)`wp-includes/post.php` |

Sets categories for a post.

  |

## 󠀁[Changelog](https://developer.wordpress.org/reference/functions/wp_create_categories/?output_format=md#changelog)󠁿

| Version | Description | 
| [2.0.0](https://developer.wordpress.org/reference/since/2.0.0/) | Introduced. |

## User Contributed Notes

You must [log in](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_create_categories%2F)
before being able to contribute a note or feedback.