Up
2
48 views
I can't find any resource or documentation that explains how to programmatically add and remove tags to a post using the post ID.

1 answer

Up
2
'Answer Accepted'
You need to first call get_object_terms to get all the terms that exist already.

function addTerm($id, $tax, $term) {

$term_id = is_term($term);
$term_id = intval($term_id);
if (!$term_id) {
$term_id = wp_insert_term($term, $tax);
$term_id = $term_id['term_id'];
$term_id = intval($term_id);
}

// get the list of terms already on this object:
$terms = wp_get_object_terms($id, $tax)
$terms[] = $term_id;

$result = wp_set_object_terms($id, $terms, $tax, FALSE);

return $result;
}

Answer this Question