wp_set_post_terms

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

WordPress Version: 6.1

/**
 * Sets the terms for a post.
 *
 * @since 2.8.0
 *
 * @see wp_set_object_terms()
 *
 * @param int          $post_id  Optional. The Post ID. Does not default to the ID of the global $post.
 * @param string|array $terms    Optional. An array of terms to set for the post, or a string of terms
 *                               separated by commas. Hierarchical taxonomies must always pass IDs rather
 *                               than names so that children with the same names but different parents
 *                               aren't confused. Default empty.
 * @param string       $taxonomy Optional. Taxonomy name. Default 'post_tag'.
 * @param bool         $append   Optional. If true, don't delete existing terms, just add on. If false,
 *                               replace the terms with the new terms. Default false.
 * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure.
 */
function wp_set_post_terms($post_id = 0, $terms = '', $taxonomy = 'post_tag', $append = false)
{
    $post_id = (int) $post_id;
    if (!$post_id) {
        return false;
    }
    if (empty($terms)) {
        $terms = array();
    }
    if (!is_array($terms)) {
        $comma = _x(',', 'tag delimiter');
        if (',' !== $comma) {
            $terms = str_replace($comma, ',', $terms);
        }
        $terms = explode(',', trim($terms, " \n\t\r\x00\v,"));
    }
    /*
     * Hierarchical taxonomies must always pass IDs rather than names so that
     * children with the same names but different parents aren't confused.
     */
    if (is_taxonomy_hierarchical($taxonomy)) {
        $terms = array_unique(array_map('intval', $terms));
    }
    return wp_set_object_terms($post_id, $terms, $taxonomy, $append);
}

WordPress Version: 5.1

/**
 * Set the terms for a post.
 *
 * @since 2.8.0
 *
 * @see wp_set_object_terms()
 *
 * @param int          $post_id  Optional. The Post ID. Does not default to the ID of the global $post.
 * @param string|array $tags     Optional. An array of terms to set for the post, or a string of terms
 *                               separated by commas. Hierarchical taxonomies must always pass IDs rather
 *                               than names so that children with the same names but different parents
 *                               aren't confused. Default empty.
 * @param string       $taxonomy Optional. Taxonomy name. Default 'post_tag'.
 * @param bool         $append   Optional. If true, don't delete existing terms, just add on. If false,
 *                               replace the terms with the new terms. Default false.
 * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure.
 */
function wp_set_post_terms($post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false)
{
    $post_id = (int) $post_id;
    if (!$post_id) {
        return false;
    }
    if (empty($tags)) {
        $tags = array();
    }
    if (!is_array($tags)) {
        $comma = _x(',', 'tag delimiter');
        if (',' !== $comma) {
            $tags = str_replace($comma, ',', $tags);
        }
        $tags = explode(',', trim($tags, " \n\t\r\x00\v,"));
    }
    /*
     * Hierarchical taxonomies must always pass IDs rather than names so that
     * children with the same names but different parents aren't confused.
     */
    if (is_taxonomy_hierarchical($taxonomy)) {
        $tags = array_unique(array_map('intval', $tags));
    }
    return wp_set_object_terms($post_id, $tags, $taxonomy, $append);
}

WordPress Version: 4.5

/**
 * Set the terms for a post.
 *
 * @since 2.8.0
 *
 * @see wp_set_object_terms()
 *
 * @param int          $post_id  Optional. The Post ID. Does not default to the ID of the global $post.
 * @param string|array $tags     Optional. An array of terms to set for the post, or a string of terms
 *                               separated by commas. Default empty.
 * @param string       $taxonomy Optional. Taxonomy name. Default 'post_tag'.
 * @param bool         $append   Optional. If true, don't delete existing terms, just add on. If false,
 *                               replace the terms with the new terms. Default false.
 * @return array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure.
 */
function wp_set_post_terms($post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false)
{
    $post_id = (int) $post_id;
    if (!$post_id) {
        return false;
    }
    if (empty($tags)) {
        $tags = array();
    }
    if (!is_array($tags)) {
        $comma = _x(',', 'tag delimiter');
        if (',' !== $comma) {
            $tags = str_replace($comma, ',', $tags);
        }
        $tags = explode(',', trim($tags, " \n\t\r\x00\v,"));
    }
    /*
     * Hierarchical taxonomies must always pass IDs rather than names so that
     * children with the same names but different parents aren't confused.
     */
    if (is_taxonomy_hierarchical($taxonomy)) {
        $tags = array_unique(array_map('intval', $tags));
    }
    return wp_set_object_terms($post_id, $tags, $taxonomy, $append);
}

WordPress Version: 4.4

/**
 * Set the terms for a post.
 *
 * @since 2.8.0
 *
 * @see wp_set_object_terms()
 *
 * @param int          $post_id  Optional. The Post ID. Does not default to the ID of the global $post.
 * @param string|array $tags     Optional. An array of terms to set for the post, or a string of terms
 *                               separated by commas. Default empty.
 * @param string       $taxonomy Optional. Taxonomy name. Default 'post_tag'.
 * @param bool         $append   Optional. If true, don't delete existing terms, just add on. If false,
 *                               replace the terms with the new terms. Default false.
 * @return array|false|WP_Error Array of affected term IDs. WP_Error or false on failure.
 */
function wp_set_post_terms($post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false)
{
    $post_id = (int) $post_id;
    if (!$post_id) {
        return false;
    }
    if (empty($tags)) {
        $tags = array();
    }
    if (!is_array($tags)) {
        $comma = _x(',', 'tag delimiter');
        if (',' !== $comma) {
            $tags = str_replace($comma, ',', $tags);
        }
        $tags = explode(',', trim($tags, " \n\t\r\x00\v,"));
    }
    /*
     * Hierarchical taxonomies must always pass IDs rather than names so that
     * children with the same names but different parents aren't confused.
     */
    if (is_taxonomy_hierarchical($taxonomy)) {
        $tags = array_unique(array_map('intval', $tags));
    }
    return wp_set_object_terms($post_id, $tags, $taxonomy, $append);
}

WordPress Version: 4.3

/**
 * Set the terms for a post.
 *
 * @since 2.8.0
 *
 * @see wp_set_object_terms()
 *
 * @param int    $post_id  Optional. The Post ID. Does not default to the ID of the global $post.
 * @param string $tags     Optional. The tags to set for the post, separated by commas. Default empty.
 * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'.
 * @param bool   $append   Optional. If true, don't delete existing tags, just add on. If false,
 *                         replace the tags with the new tags. Default false.
 * @return array|false|WP_Error Array of affected term IDs. WP_Error or false on failure.
 */
function wp_set_post_terms($post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false)
{
    $post_id = (int) $post_id;
    if (!$post_id) {
        return false;
    }
    if (empty($tags)) {
        $tags = array();
    }
    if (!is_array($tags)) {
        $comma = _x(',', 'tag delimiter');
        if (',' !== $comma) {
            $tags = str_replace($comma, ',', $tags);
        }
        $tags = explode(',', trim($tags, " \n\t\r\x00\v,"));
    }
    /*
     * Hierarchical taxonomies must always pass IDs rather than names so that
     * children with the same names but different parents aren't confused.
     */
    if (is_taxonomy_hierarchical($taxonomy)) {
        $tags = array_unique(array_map('intval', $tags));
    }
    return wp_set_object_terms($post_id, $tags, $taxonomy, $append);
}

WordPress Version: 4.0

/**
 * Set the terms for a post.
 *
 * @since 2.8.0
 *
 * @see wp_set_object_terms()
 *
 * @param int    $post_id  Optional. The Post ID. Does not default to the ID of the global $post.
 * @param string $tags     Optional. The tags to set for the post, separated by commas. Default empty.
 * @param string $taxonomy Optional. Taxonomy name. Default 'post_tag'.
 * @param bool   $append   Optional. If true, don't delete existing tags, just add on. If false,
 *                         replace the tags with the new tags. Default false.
 * @return mixed Array of affected term IDs. WP_Error or false on failure.
 */
function wp_set_post_terms($post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false)
{
    $post_id = (int) $post_id;
    if (!$post_id) {
        return false;
    }
    if (empty($tags)) {
        $tags = array();
    }
    if (!is_array($tags)) {
        $comma = _x(',', 'tag delimiter');
        if (',' !== $comma) {
            $tags = str_replace($comma, ',', $tags);
        }
        $tags = explode(',', trim($tags, " \n\t\r\x00\v,"));
    }
    /*
     * Hierarchical taxonomies must always pass IDs rather than names so that
     * children with the same names but different parents aren't confused.
     */
    if (is_taxonomy_hierarchical($taxonomy)) {
        $tags = array_unique(array_map('intval', $tags));
    }
    return wp_set_object_terms($post_id, $tags, $taxonomy, $append);
}

WordPress Version: 3.7

/**
 * Set the terms for a post.
 *
 * @since 2.8.0
 * @uses wp_set_object_terms() Sets the tags for the post.
 *
 * @param int $post_id Post ID.
 * @param string $tags The tags to set for the post, separated by commas.
 * @param string $taxonomy Taxonomy name. Defaults to 'post_tag'.
 * @param bool $append If true, don't delete existing tags, just add on. If false, replace the tags with the new tags.
 * @return mixed Array of affected term IDs. WP_Error or false on failure.
 */
function wp_set_post_terms($post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false)
{
    $post_id = (int) $post_id;
    if (!$post_id) {
        return false;
    }
    if (empty($tags)) {
        $tags = array();
    }
    if (!is_array($tags)) {
        $comma = _x(',', 'tag delimiter');
        if (',' !== $comma) {
            $tags = str_replace($comma, ',', $tags);
        }
        $tags = explode(',', trim($tags, " \n\t\r\x00\v,"));
    }
    // Hierarchical taxonomies must always pass IDs rather than names so that children with the same
    // names but different parents aren't confused.
    if (is_taxonomy_hierarchical($taxonomy)) {
        $tags = array_unique(array_map('intval', $tags));
    }
    return wp_set_object_terms($post_id, $tags, $taxonomy, $append);
}