Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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".

Thanks.

share|improve this question
    
Are you talking about external api-requests like XML-RPC or the WordPress-api for adding/deleting tags to posts? –  Pontus Abrahamsson Jan 21 '13 at 11:08
    
Looking for the "wp_function" function –  Prakash Raman Jan 21 '13 at 14:20

3 Answers 3

up vote 5 down vote accepted

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);
share|improve this answer
    
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. –  Prakash Raman Jan 21 '13 at 14:20
    
@PrakashRaman: please see updated answer –  webaware Jan 22 '13 at 0:03
1  
Thanks yep. That works ! –  Prakash Raman Jan 22 '13 at 19:43

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);
share|improve this answer
1  
Thanks. This helped. –  Prakash Raman Jan 22 '13 at 19:45

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

share|improve this answer
    
thanks, but am looking for setting tags to a post. –  Prakash Raman Jan 21 '13 at 13:56
    
@PrakashRaman This will set tags to a post. Just replace category with post-tag. –  kaiser Jan 21 '13 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)... –  Oleg Butuzov Jan 21 '13 at 15:09

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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