Function: wp_set_object_terms

wp_set_object_terms( integer $object_id, string | integer | array $terms, string $taxonomy, boolean $append )

Create Term and Taxonomy Relationships.

Shortcut: wsot

You can use above key combination to trigger the associated snippet for this function in the WordPress Snippets for Visual Studio Code extension. You can also use this shortcut to search for this function on this website.

Relates an object (post, link etc) to a term and taxonomy type. Creates the term and taxonomy relationship if it doesn't already exist. Creates a term if it doesn't exist (using the slug).

A relationship means that the term is grouped in or belongs to the taxonomy. A term has no meaning until it is given context by defining which taxonomy it exists under.

Parameters

Name Type(s) Default Value Description
$object_id integer

The object to relate to.

$terms string | integer | array

A single term slug, single term id, or array of either term slugs or ids. Will replace all existing related terms in this taxonomy. Passing an empty value will remove all related terms.

$taxonomy string

The context in which to relate the term to the object.

$append boolean false

Optional. If false will delete difference of terms. Default false.

Returns

array | WP_Error

Term taxonomy IDs of the affected terms.

WordPress Developer Newsletter

Stay informed of new chapter releases, important WordPress API updates and more.