wp_create_categories

Create categories for the given post.

Description

(array) wp_create_categories( (array) $categories, (string) $post_id = '' ); 

Returns (array)

List of categories to create for the given post.

Parameters (2)

0. $categories (array)
List of categories to create.
1. $post_id — Optional. (string) => ''
The post ID. Default empty.

Usage

  1. if ( !function_exists( 'wp_create_categories' ) ) { 
  2. require_once ABSPATH . '/wp-admin/includes/taxonomy.php'; 
  3.  
  4. // List of categories to create. 
  5. $categories = array(); 
  6.  
  7. // Optional. The post ID. Default empty. 
  8. $post_id = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = wp_create_categories($categories, $post_id); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-admin/includes/taxonomy.php  
  1. function wp_create_categories( $categories, $post_id = '' ) { 
  2. $cat_ids = array (); 
  3. foreach ( $categories as $category ) { 
  4. if ( $id = category_exists( $category ) ) { 
  5. $cat_ids[] = $id; 
  6. } elseif ( $id = wp_create_category( $category ) ) { 
  7. $cat_ids[] = $id; 
  8.  
  9. if ( $post_id ) 
  10. wp_set_post_categories($post_id, $cat_ids); 
  11.  
  12. return $cat_ids;