wp_set_object_terms()

This WordPress function requires the CPT-onomy class.

Description

Relates an object (post) to a term and CPT-onomy. Creates the term and CPT-onomy relationship if it doesn’t already exist.

Usage

$cpt_onomy->wp_set_object_terms( 						int
						$object_id
						, 						int|array|string
						$terms
						, 						string|array
						$cpt_onomy
						, 						bool
						$append
						 );

Parameters

$object_id (integer) (required)
Default: none
The object's ID.

$terms (integer|array|string) (required)
Default: none
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)
Default: none
Name of the CPT-onomy that the term(s) belong to.

$append (boolean) (optional)
Default: false
If false will delete difference of terms.

Return Values

(array|WP_Error) Affected term IDs.

Examples

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' );
?>

WordPress Codex

For more information, refer to the WordPress codex.