I have been googling around but could not find the "add tag to post" api/codex. Does anyone know what it is ? Also, the "delete tag from post".


  • Are you talking about external api-requests like XML-RPC or the WordPress-api for adding/deleting tags to posts? Jan 21, 2013 at 11:08
  • Looking for the "wp_function" function Jan 21, 2013 at 14:20

3 Answers 3


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) {

// set new list of tags, without $target
wp_set_post_tags($post->ID, $tags, false);
  • Great, thanks. Just what I needed to know. Is there a way to delete/unset a tag from a post ? But not from the terms pool. Jan 21, 2013 at 14:20
  • @PrakashRaman: please see updated answer
    – webaware
    Jan 22, 2013 at 0:03

You would use the wp_insert_term() function to insert a new tag:

wp_insert_term( 'post_tag', 'happy' );

Then after creating your terms, you would add them to the post of your choice using the wp_set_post_terms() function, like so:

wp_set_post_terms( $post_id, 'happy', 'post_tag', true);

Google Knows it.

If you wanted to add a categories to a post with the ID of 42:

$cat_ids = array( 6,8 );
    //to make sure the terms IDs is integers:
    //$cat_ids = array_map('intval', $cat_ids);
    //$cat_ids = array_unique( $cat_ids );
wp_set_object_terms( '42', $cat_ids, 'category' );

If you wanted to clear/remove all categories from a post with the ID of 42:

wp_set_object_terms( '42', NULL, 'category' );

read more about wp_set_object_terms

  • thanks, but am looking for setting tags to a post. Jan 21, 2013 at 13:56
  • @PrakashRaman This will set tags to a post. Just replace category with post-tag.
    – kaiser
    Jan 21, 2013 at 15:01
  • Basicly its example from page that overyone here referenceing... category is same term as post_tag just hierarchical one. take a look to any of links posted here (anyway thay all points to one place)... Jan 21, 2013 at 15:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.