wp_create_categories

Advertisement

Summery Summery

Create categories for the given post.

Syntax Syntax

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

Parameters Parameters

$categories

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

$post_id

(int) (Optional) The post ID.

Default value: ''

Return Return

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

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

Advertisement

Changelog Changelog

Changelog
Version Description
2.0.0 Introduced.

Advertisement

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.