wp_create_categories() WordPress Function

The wp_create_categories() function allows you to create new categories for your posts and pages. This function takes two arguments: the name of the category and the parent category. The name argument is required, while the parent category is optional. If you do not specify a parent category, the category will be created at the top level.

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

Create categories for the given post.


Parameters

$categories

(string[])(Required)Array of category names to create.

$post_id

(int)(Optional) The post ID.

Default value: ''


Top ↑

Return

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


Top ↑

Source

File: wp-admin/includes/taxonomy.php

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


Top ↑

Changelog

Changelog
VersionDescription
2.0.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.