Add tag to post api wordpress

You’ll find an index of a good chunk of the WordPress API here on codex. The function you want is wp_set_post_tags(), but follow the links from that page to related functions.

Edit: this should remove a tag from a post, per comment below

// $post is your post object, e.g. from: global $post;
// $target is tag you want to remove

// get an array of current tags on post
$tags = wp_get_post_tags($post->ID, array('fields' => 'names'));

// remove selected tag from array
$key = array_search($target, $tags);
if ($key !== false) {
    unset($tags[$key]);
}

// set new list of tags, without $target
wp_set_post_tags($post->ID, $tags, false);

Leave a Comment