Title: wp_create_category
Published: April 25, 2014
Last modified: August 20, 2026

---

# wp_create_category( string $category_name, int $category_parent ): int

## In this article

 * [Parameters](https://developer.wordpress.org/reference/functions/wp_create_category/?output_format=md#parameters)
 * [Return](https://developer.wordpress.org/reference/functions/wp_create_category/?output_format=md#return)
 * [More Information](https://developer.wordpress.org/reference/functions/wp_create_category/?output_format=md#more-information)
 * [Source](https://developer.wordpress.org/reference/functions/wp_create_category/?output_format=md#source)
 * [Related](https://developer.wordpress.org/reference/functions/wp_create_category/?output_format=md#related)
 * [Changelog](https://developer.wordpress.org/reference/functions/wp_create_category/?output_format=md#changelog)
 * [User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_create_category/?output_format=md#user-contributed-notes)

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

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

 `$category_name`stringrequired

Category name.

`$category_parent`intoptional

ID of parent category.

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

 int The ID of category term on success, or zero on failure.

## 󠀁[More Information](https://developer.wordpress.org/reference/functions/wp_create_category/?output_format=md#more-information)󠁿

Parameters:

 * `$cat_name`: Name for the new category.
 * `$parent`: Category ID of the parent category.

Returns:

 * 0 on failure, category id on success.

[wp_create_category()](https://developer.wordpress.org/reference/functions/wp_create_category/)
is a thin wrapper around [wp_insert_category()](https://codex.wordpress.org/Function_Reference/wp_insert_category).

Because this is a wrapper, it is not suitable for entering a complex custom taxonomy
element.

If the category already exists, it is not duplicated. The ID of the original existing
category is returned without error.

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

    ```php
    function wp_create_category( $category_name, $category_parent = 0 ) {
    	$id = category_exists( $category_name, $category_parent );
    	if ( $id ) {
    		return (int) $id;
    	}

    	return wp_insert_category(
    		array(
    			'cat_name'        => $category_name,
    			'category_parent' => $category_parent,
    		)
    	);
    }
    ```

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

## 󠀁[Related](https://developer.wordpress.org/reference/functions/wp_create_category/?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_insert_category()](https://developer.wordpress.org/reference/functions/wp_insert_category/)`wp-admin/includes/taxonomy.php` |

Updates an existing Category or creates a new Category.

  |

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

Creates categories for the given post.

  |

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

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

## 󠀁[User Contributed Notes](https://developer.wordpress.org/reference/functions/wp_create_category/?output_format=md#user-contributed-notes)󠁿

 1.   [Skip to note 3 content](https://developer.wordpress.org/reference/functions/wp_create_category/?output_format=md#comment-content-6705)
 2.    [Muhibul Haque](https://profiles.wordpress.org/devmuhib/)  [  3 years ago  ](https://developer.wordpress.org/reference/functions/wp_create_category/#comment-6705)
 3.  [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_create_category%2F%23comment-6705)
     Vote results for this note: 0[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_create_category%2F%23comment-6705)
 4.  An example of how you can use the **[wp_create_category()](https://developer.wordpress.org/reference/functions/wp_create_category/)**
     function.
 5.      ```php
         $category_name = 'New Category'; // Name of the category you want to create
         $parent_category_id = 3; // ID of the parent category (change this to your desired parent category)
     
         // Attempt to create a new category
         $category_id = wp_create_category( $category_name, $parent_category_id );
     
         if ( ! is_wp_error( $category_id ) ) {
             echo 'Category created with ID: ' . $category_id;
         } else {
             echo 'Error creating category: ' . $category_id->get_error_message();
         }
         ```
     
 6.  **In this example:**
 7.  `$category_name` is set to “New Category”, which is the name of the category you
     want to create.
      `$parent_category_id` is set to 3, which is the ID of the parent
     category where you want to create the new category. You should replace this with
     the actual ID of the desired parent category. The `wp_create_category()` function
     is called with both the category name and the parent category ID as arguments.
     If the category is successfully created, it will return the category ID as an 
     integer. If there’s an error during the creation process, it will return a `WP_Error`
     object.
 8.  The code then checks if the result is a `WP_Error` object using `is_wp_error()`.
     If it’s not an error, it means the category was created successfully, and it will
     display a message with the category ID. If an error occurred, it will display 
     an error message using the `get_error_message()` method of the `WP_Error` object.
 9.   [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_create_category%2F%3Freplytocom%3D6705%23feedback-editor-6705)
 10.  [Skip to note 4 content](https://developer.wordpress.org/reference/functions/wp_create_category/?output_format=md#comment-content-723)
 11.   [Codex](https://profiles.wordpress.org/codex/)  [  11 years ago  ](https://developer.wordpress.org/reference/functions/wp_create_category/#comment-723)
 12. [You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_create_category%2F%23comment-723)
     Vote results for this note: -1[You must log in to vote on the helpfulness of this note](https://login.wordpress.org?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_create_category%2F%23comment-723)
 13. **Examples**
 14. In order to create a simple category use:
 15.     ```php
          wp_create_category( 'My category name' );
         ```
     
 16. To create a category that is a child of Uncategorized (or whatever category has
     the ID 0), use:
 17.     ```php
         wp_create_category( 'Child of Uncategorized', 0 );
         ```
     
 18. To get id of category created and put value in variable:
 19.     ```php
         $id = wp_create_category( 'Child of Uncategorized', 0 );
         ```
     
 20.  [Log in to add feedback](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fdeveloper.wordpress.org%2Freference%2Ffunctions%2Fwp_create_category%2F%3Freplytocom%3D723%23feedback-editor-723)

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