wp_set_post_categories

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

WordPress Version: 6.2

/**
 * Sets categories for a post.
 *
 * If no categories are provided, the default category is used.
 *
 * @since 2.1.0
 *
 * @param int       $post_id         Optional. The Post ID. Does not default to the ID
 *                                   of the global $post. Default 0.
 * @param int[]|int $post_categories Optional. List of category IDs, or the ID of a single category.
 *                                   Default empty array.
 * @param bool      $append          If true, don't delete existing categories, just add on.
 *                                   If false, replace the categories with the new categories.
 * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure.
 */
function wp_set_post_categories($post_id = 0, $post_categories = array(), $append = false)
{
    $post_id = (int) $post_id;
    $post_type = get_post_type($post_id);
    $post_status = get_post_status($post_id);
    // If $post_categories isn't already an array, make it one.
    $post_categories = (array) $post_categories;
    if (empty($post_categories)) {
        /**
         * Filters post types (in addition to 'post') that require a default category.
         *
         * @since 5.5.0
         *
         * @param string[] $post_types An array of post type names. Default empty array.
         */
        $default_category_post_types = apply_filters('default_category_post_types', array());
        // Regular posts always require a default category.
        $default_category_post_types = array_merge($default_category_post_types, array('post'));
        if (in_array($post_type, $default_category_post_types, true) && is_object_in_taxonomy($post_type, 'category') && 'auto-draft' !== $post_status) {
            $post_categories = array(get_option('default_category'));
            $append = false;
        } else {
            $post_categories = array();
        }
    } elseif (1 === count($post_categories) && '' === reset($post_categories)) {
        return true;
    }
    return wp_set_post_terms($post_id, $post_categories, 'category', $append);
}

WordPress Version: 6.1

/**
 * Sets categories for a post.
 *
 * If no categories are provided, the default category is used.
 *
 * @since 2.1.0
 *
 * @param int       $post_ID         Optional. The Post ID. Does not default to the ID
 *                                   of the global $post. Default 0.
 * @param int[]|int $post_categories Optional. List of category IDs, or the ID of a single category.
 *                                   Default empty array.
 * @param bool      $append          If true, don't delete existing categories, just add on.
 *                                   If false, replace the categories with the new categories.
 * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure.
 */
function wp_set_post_categories($post_ID = 0, $post_categories = array(), $append = false)
{
    $post_ID = (int) $post_ID;
    $post_type = get_post_type($post_ID);
    $post_status = get_post_status($post_ID);
    // If $post_categories isn't already an array, make it one.
    $post_categories = (array) $post_categories;
    if (empty($post_categories)) {
        /**
         * Filters post types (in addition to 'post') that require a default category.
         *
         * @since 5.5.0
         *
         * @param string[] $post_types An array of post type names. Default empty array.
         */
        $default_category_post_types = apply_filters('default_category_post_types', array());
        // Regular posts always require a default category.
        $default_category_post_types = array_merge($default_category_post_types, array('post'));
        if (in_array($post_type, $default_category_post_types, true) && is_object_in_taxonomy($post_type, 'category') && 'auto-draft' !== $post_status) {
            $post_categories = array(get_option('default_category'));
            $append = false;
        } else {
            $post_categories = array();
        }
    } elseif (1 === count($post_categories) && '' === reset($post_categories)) {
        return true;
    }
    return wp_set_post_terms($post_ID, $post_categories, 'category', $append);
}

WordPress Version: 5.7

/**
 * Set categories for a post.
 *
 * If no categories are provided, the default category is used.
 *
 * @since 2.1.0
 *
 * @param int       $post_ID         Optional. The Post ID. Does not default to the ID
 *                                   of the global $post. Default 0.
 * @param int[]|int $post_categories Optional. List of category IDs, or the ID of a single category.
 *                                   Default empty array.
 * @param bool      $append          If true, don't delete existing categories, just add on.
 *                                   If false, replace the categories with the new categories.
 * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure.
 */
function wp_set_post_categories($post_ID = 0, $post_categories = array(), $append = false)
{
    $post_ID = (int) $post_ID;
    $post_type = get_post_type($post_ID);
    $post_status = get_post_status($post_ID);
    // If $post_categories isn't already an array, make it one.
    $post_categories = (array) $post_categories;
    if (empty($post_categories)) {
        /**
         * Filters post types (in addition to 'post') that require a default category.
         *
         * @since 5.5.0
         *
         * @param string[] $post_types An array of post type names. Default empty array.
         */
        $default_category_post_types = apply_filters('default_category_post_types', array());
        // Regular posts always require a default category.
        $default_category_post_types = array_merge($default_category_post_types, array('post'));
        if (in_array($post_type, $default_category_post_types, true) && is_object_in_taxonomy($post_type, 'category') && 'auto-draft' !== $post_status) {
            $post_categories = array(get_option('default_category'));
            $append = false;
        } else {
            $post_categories = array();
        }
    } elseif (1 === count($post_categories) && '' === reset($post_categories)) {
        return true;
    }
    return wp_set_post_terms($post_ID, $post_categories, 'category', $append);
}

WordPress Version: 5.5

/**
 * Set categories for a post.
 *
 * If no categories are provided, the default category is used.
 *
 * @since 2.1.0
 *
 * @param int       $post_ID         Optional. The Post ID. Does not default to the ID
 *                                   of the global $post. Default 0.
 * @param array|int $post_categories Optional. List of category IDs, or the ID of a single category.
 *                                   Default empty array.
 * @param bool      $append          If true, don't delete existing categories, just add on.
 *                                   If false, replace the categories with the new categories.
 * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure.
 */
function wp_set_post_categories($post_ID = 0, $post_categories = array(), $append = false)
{
    $post_ID = (int) $post_ID;
    $post_type = get_post_type($post_ID);
    $post_status = get_post_status($post_ID);
    // If $post_categories isn't already an array, make it one.
    $post_categories = (array) $post_categories;
    if (empty($post_categories)) {
        /**
         * Filters post types (in addition to 'post') that require a default category.
         *
         * @since 5.5.0
         *
         * @param string[] $post_types An array of post type names. Default empty array.
         */
        $default_category_post_types = apply_filters('default_category_post_types', array());
        // Regular posts always require a default category.
        $default_category_post_types = array_merge($default_category_post_types, array('post'));
        if (in_array($post_type, $default_category_post_types, true) && is_object_in_taxonomy($post_type, 'category') && 'auto-draft' !== $post_status) {
            $post_categories = array(get_option('default_category'));
            $append = false;
        } else {
            $post_categories = array();
        }
    } elseif (1 === count($post_categories) && '' === reset($post_categories)) {
        return true;
    }
    return wp_set_post_terms($post_ID, $post_categories, 'category', $append);
}

WordPress Version: 5.1

/**
 * Set categories for a post.
 *
 * If the post categories parameter is not set, then the default category is
 * going used.
 *
 * @since 2.1.0
 *
 * @param int       $post_ID         Optional. The Post ID. Does not default to the ID
 *                                   of the global $post. Default 0.
 * @param array|int $post_categories Optional. List of category IDs, or the ID of a single category.
 *                                   Default empty array.
 * @param bool      $append          If true, don't delete existing categories, just add on.
 *                                   If false, replace the categories with the new categories.
 * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure.
 */
function wp_set_post_categories($post_ID = 0, $post_categories = array(), $append = false)
{
    $post_ID = (int) $post_ID;
    $post_type = get_post_type($post_ID);
    $post_status = get_post_status($post_ID);
    // If $post_categories isn't already an array, make it one:
    $post_categories = (array) $post_categories;
    if (empty($post_categories)) {
        if ('post' == $post_type && 'auto-draft' != $post_status) {
            $post_categories = array(get_option('default_category'));
            $append = false;
        } else {
            $post_categories = array();
        }
    } elseif (1 == count($post_categories) && '' == reset($post_categories)) {
        return true;
    }
    return wp_set_post_terms($post_ID, $post_categories, 'category', $append);
}

WordPress Version: 4.5

/**
 * Set categories for a post.
 *
 * If the post categories parameter is not set, then the default category is
 * going used.
 *
 * @since 2.1.0
 *
 * @param int       $post_ID         Optional. The Post ID. Does not default to the ID
 *                                   of the global $post. Default 0.
 * @param array|int $post_categories Optional. List of categories or ID of category.
 *                                   Default empty array.
 * @param bool      $append         If true, don't delete existing categories, just add on.
 *                                  If false, replace the categories with the new categories.
 * @return array|false|WP_Error Array of term taxonomy IDs of affected categories. WP_Error or false on failure.
 */
function wp_set_post_categories($post_ID = 0, $post_categories = array(), $append = false)
{
    $post_ID = (int) $post_ID;
    $post_type = get_post_type($post_ID);
    $post_status = get_post_status($post_ID);
    // If $post_categories isn't already an array, make it one:
    $post_categories = (array) $post_categories;
    if (empty($post_categories)) {
        if ('post' == $post_type && 'auto-draft' != $post_status) {
            $post_categories = array(get_option('default_category'));
            $append = false;
        } else {
            $post_categories = array();
        }
    } elseif (1 == count($post_categories) && '' == reset($post_categories)) {
        return true;
    }
    return wp_set_post_terms($post_ID, $post_categories, 'category', $append);
}

WordPress Version: 4.3

/**
 * Set categories for a post.
 *
 * If the post categories parameter is not set, then the default category is
 * going used.
 *
 * @since 2.1.0
 *
 * @param int       $post_ID         Optional. The Post ID. Does not default to the ID
 *                                   of the global $post. Default 0.
 * @param array|int $post_categories Optional. List of categories or ID of category.
 *                                   Default empty array.
 * @param bool      $append         If true, don't delete existing categories, just add on.
 *                                  If false, replace the categories with the new categories.
 * @return array|bool|WP_Error
 */
function wp_set_post_categories($post_ID = 0, $post_categories = array(), $append = false)
{
    $post_ID = (int) $post_ID;
    $post_type = get_post_type($post_ID);
    $post_status = get_post_status($post_ID);
    // If $post_categories isn't already an array, make it one:
    $post_categories = (array) $post_categories;
    if (empty($post_categories)) {
        if ('post' == $post_type && 'auto-draft' != $post_status) {
            $post_categories = array(get_option('default_category'));
            $append = false;
        } else {
            $post_categories = array();
        }
    } elseif (1 == count($post_categories) && '' == reset($post_categories)) {
        return true;
    }
    return wp_set_post_terms($post_ID, $post_categories, 'category', $append);
}

WordPress Version: 4.2

/**
 * Set categories for a post.
 *
 * If the post categories parameter is not set, then the default category is
 * going used.
 *
 * @since 2.1.0
 *
 * @param int       $post_ID         Optional. The Post ID. Does not default to the ID
 *                                   of the global $post. Default 0.
 * @param array|int $post_categories Optional. List of categories or ID of category.
 *                                   Default empty array.
 * @param bool      $append         If true, don't delete existing categories, just add on.
 *                                  If false, replace the categories with the new categories.
 * @return bool|mixed
 */
function wp_set_post_categories($post_ID = 0, $post_categories = array(), $append = false)
{
    $post_ID = (int) $post_ID;
    $post_type = get_post_type($post_ID);
    $post_status = get_post_status($post_ID);
    // If $post_categories isn't already an array, make it one:
    $post_categories = (array) $post_categories;
    if (empty($post_categories)) {
        if ('post' == $post_type && 'auto-draft' != $post_status) {
            $post_categories = array(get_option('default_category'));
            $append = false;
        } else {
            $post_categories = array();
        }
    } elseif (1 == count($post_categories) && '' == reset($post_categories)) {
        return true;
    }
    return wp_set_post_terms($post_ID, $post_categories, 'category', $append);
}

WordPress Version: 4.0

/**
 * Set categories for a post.
 *
 * If the post categories parameter is not set, then the default category is
 * going used.
 *
 * @since 2.1.0
 *
 * @param int       $post_ID         Optional. The Post ID. Does not default to the ID
 *                                   of the global $post. Default 0.
 * @param array|int $post_categories Optional. List of categories or ID of category.
 *                                   Default empty array.
 * @param bool      $append         If true, don't delete existing categories, just add on.
 *                                  If false, replace the categories with the new categories.
 * @return bool|mixed
 */
function wp_set_post_categories($post_ID = 0, $post_categories = array(), $append = false)
{
    $post_ID = (int) $post_ID;
    $post_type = get_post_type($post_ID);
    $post_status = get_post_status($post_ID);
    // If $post_categories isn't already an array, make it one:
    $post_categories = (array) $post_categories;
    if (empty($post_categories)) {
        if ('post' == $post_type && 'auto-draft' != $post_status) {
            $post_categories = array(get_option('default_category'));
            $append = false;
        } else {
            $post_categories = array();
        }
    } else if (1 == count($post_categories) && '' == reset($post_categories)) {
        return true;
    }
    return wp_set_post_terms($post_ID, $post_categories, 'category', $append);
}

WordPress Version: 3.7

/**
 * Set categories for a post.
 *
 * If the post categories parameter is not set, then the default category is
 * going used.
 *
 * @since 2.1.0
 *
 * @param int $post_ID Post ID.
 * @param array|int $post_categories Optional. List of categories or ID of category.
 * @param bool $append If true, don't delete existing categories, just add on. If false, replace the categories with the new categories.
 * @return bool|mixed
 */
function wp_set_post_categories($post_ID = 0, $post_categories = array(), $append = false)
{
    $post_ID = (int) $post_ID;
    $post_type = get_post_type($post_ID);
    $post_status = get_post_status($post_ID);
    // If $post_categories isn't already an array, make it one:
    $post_categories = (array) $post_categories;
    if (empty($post_categories)) {
        if ('post' == $post_type && 'auto-draft' != $post_status) {
            $post_categories = array(get_option('default_category'));
            $append = false;
        } else {
            $post_categories = array();
        }
    } else if (1 == count($post_categories) && '' == reset($post_categories)) {
        return true;
    }
    return wp_set_post_terms($post_ID, $post_categories, 'category', $append);
}