wp_create_categories()WP 2.0.0

Creates categories for the given post.

Хуков нет.

Возвращает

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

Использование

wp_create_categories( $categories, $post_id );
$categories(string[]) (обязательный)
Array of category names to create.
$post_id(int)
The post ID.
По умолчанию: ''

Список изменений

С версии 2.0.0 Введена.

Код wp_create_categories() WP 6.4.3

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