A WordPress-centric search engine for devs and theme authors



wp_create_categories ›

Since2.0.0
Deprecatedn/a
wp_create_categories ( $categories, $post_id = '' )
Parameters: (2)
  • (string[]) $categories Array of category names to create.
    Required: Yes
  • (int) $post_id Optional. The post ID. Default empty.
    Required: No
    Default: (empty)
Returns:
  • (array) List of categories to create for the given post.
Defined at:
Codex:

Create categories for the given post.



Source

function wp_create_categories( $categories, $post_id = '' ) {
	$cat_ids = array();
	foreach ( $categories as $category ) {
		if ( $id = category_exists( $category ) ) {
			$cat_ids[] = $id;
		} elseif ( $id = wp_create_category( $category ) ) {
			$cat_ids[] = $id;
		}
	}

	if ( $post_id ) {
		wp_set_post_categories( $post_id, $cat_ids );
	}

	return $cat_ids;
}