wp_create_categories

The timeline below displays how wordpress function wp_create_categories has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.1

/**
 * Creates categories for the given post.
 *
 * @since 2.0.0
 *
 * @param string[] $categories Array of category names to create.
 * @param int      $post_id    Optional. The post ID. Default empty.
 * @return int[] Array of IDs of categories assigned to the given post.
 */
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;
}

WordPress Version: 5.4

/**
 * Create categories for the given post.
 *
 * @since 2.0.0
 *
 * @param string[] $categories Array of category names to create.
 * @param int      $post_id    Optional. The post ID. Default empty.
 * @return int[] Array of IDs of categories assigned to the given post.
 */
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;
}

WordPress Version: 5.3

/**
 * Create categories for the given post.
 *
 * @since 2.0.0
 *
 * @param string[] $categories Array of category names to create.
 * @param int      $post_id    Optional. The post ID. Default empty.
 * @return array List of categories to create for the given post.
 */
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;
}

WordPress Version: 5.1

/**
 * Create categories for the given post.
 *
 * @since 2.0.0
 *
 * @param string[] $categories Array of category names to create.
 * @param int      $post_id    Optional. The post ID. Default empty.
 * @return array List of categories to create for the given post.
 */
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;
}

WordPress Version: 4.5

/**
 * Create categories for the given post.
 *
 * @since 2.0.0
 *
 * @param array $categories List of categories to create.
 * @param int   $post_id    Optional. The post ID. Default empty.
 * @return array List of categories to create for the given post.
 */
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;
}

WordPress Version: 4.2

/**
 * Create categories for the given post.
 *
 * @since 2.0.0
 *
 * @param array $categories List of categories to create.
 * @param int   $post_id    Optional. The post ID. Default empty.
 * @return List of categories to create for the given post.
 */
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;
}

WordPress Version: 4.0

/**
 * Create categories for the given post.
 *
 * @since 2.0.0
 *
 * @param array $categories List of categories to create.
 * @param int   $post_id    Optional. The post ID. Default empty.
 * @return List of categories to create for the given post.
 */
function wp_create_categories($categories, $post_id = '')
{
    $cat_ids = array();
    foreach ($categories as $category) {
        if ($id = category_exists($category)) {
            $cat_ids[] = $id;
        } else if ($id = wp_create_category($category)) {
            $cat_ids[] = $id;
        }
    }
    if ($post_id) {
        wp_set_post_categories($post_id, $cat_ids);
    }
    return $cat_ids;
}

WordPress Version: 3.7

/**
 * {@internal Missing Short Description}}
 *
 * @since 2.0.0
 *
 * @param unknown_type $categories
 * @param unknown_type $post_id
 * @return unknown
 */
function wp_create_categories($categories, $post_id = '')
{
    $cat_ids = array();
    foreach ($categories as $category) {
        if ($id = category_exists($category)) {
            $cat_ids[] = $id;
        } else if ($id = wp_create_category($category)) {
            $cat_ids[] = $id;
        }
    }
    if ($post_id) {
        wp_set_post_categories($post_id, $cat_ids);
    }
    return $cat_ids;
}