wp_insert_category example


function wp_create_category( $cat_name$category_parent = 0 ) {
    $id = category_exists( $cat_name$category_parent );
    if ( $id ) {
        return $id;
    }

    return wp_insert_category(
        array(
            'cat_name'        => $cat_name,
            'category_parent' => $category_parent,
        )
    );
}

/** * 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. */
        if ( empty( $category['description'] ) ) {
            $category['description'] = '';
        }

        $new_category = array(
            'cat_name'             => $category['name'],
            'category_nicename'    => $category['slug'],
            'category_parent'      => $category['parent_id'],
            'category_description' => $category['description'],
        );

        $cat_id = wp_insert_category( $new_category, true );
        if ( is_wp_error( $cat_id ) ) {
            if ( 'term_exists' === $cat_id->get_error_code() ) {
                return (int) $cat_id->get_error_data();
            } else {
                return new IXR_Error( 500, __( 'Sorry, the category could not be created.' ) );
            }
        } elseif ( ! $cat_id ) {
            return new IXR_Error( 500, __( 'Sorry, the category could not be created.' ) );
        }

        /** * Fires after a new category has been successfully created via XML-RPC. * * @since 3.4.0 * * @param int $cat_id ID of the new category. * @param array $args An array of new category arguments. */
Home | Imprint | This part of the site doesn't use cookies.