Relates an object (post) to a term and CPT-onomy. Creates the term and CPT-onomy relationship if it doesn’t already exist.
$cpt_onomy->wp_set_object_terms( int $object_id , int|array|string $terms , string|array $cpt_onomy , bool $append );
$object_id (integer) (required)
The object's ID.
$terms (integer|array|string) (required)
The slug or id of the term, will replace all existing related terms in this CPT-onomy. To clear or remove all terms from an object, pass an empty string or NULL. Function intval() is required in term IDs.
$cpt_onomy (string|array) (required)
Name of the CPT-onomy that the term(s) belong to.
$append (boolean) (optional)
If false will delete difference of terms.
(array|WP_Error) Affected term IDs.
If you wanted to create a relationship between the post ID 42 and the 'actors' CPT-onomy term with the slug 'tom-hanks':
<?php global $cpt_onomy; $cpt_onomy->wp_set_object_terms( 42, 'tom-hanks', 'actors' ); ?>
If you wanted to clear/remove all 'actors' from a post with the ID of 42:
<?php global $cpt_onomy; $cpt_onomy->wp_set_object_terms( 42, NULL, 'actors' ); ?>
For more information, refer to the WordPress codex.