wpseek.com
A WordPress-centric search engine for devs and theme authors



wp_create_categories › WordPress Function

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:
  • (int[]) Array of IDs of categories assigned to the given post.
Defined at:
Codex:

Creates categories for the given post.



Source

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