wp_set_post_terms()

This WordPress function requires the CPT-onomy class.

Description

This function sets the terms for a post.

Usage

$cpt_onomy->wp_set_post_terms( 						int
						$post_id 
						, 						string|array
						$terms
						, 						string
						$taxonomy
						, 						bool
						$append
						 );

Parameters

$post_id (integer) (required)
Default: none
Post ID

$terms (string|array) (required)
Default: none
The terms to set for the post. Can be an array or a comma separated string. Hierarchical taxonomies must always pass IDs.

$taxonomy (string) (required)
Default: none
Taxonomy name

$append (boolean) (optional)
Default: false
If true, don't delete existing terms, just add on. If false, replace the terms with the new terms.

Examples

If you wanted to create a relationship between the post ID 42 and the 'actors' CPT-onomy term with the slug 'brad-pitt', but make sure all other relationships stay intact:

<?php
global $cpt_onomy;
$cpt_onomy->wp_set_post_terms( 42, array( 'brad-pitt' ), 'actors', true );
?>

If you wanted to create a relationship between the post ID 42 and the 'actors' CPT-onomy term with the slug 'tom-hanks', but erase all other relationships:

<?php
global $cpt_onomy;
$cpt_onomy->wp_set_post_terms( 42, array( '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_post_terms( 42, NULL, 'actors' );
?>

Notes

  • Since 1.3

WordPress Codex

For more information, refer to the WordPress codex.