WordPress at a glance

wp_set_post_terms() WP 1.0

Set the terms for a post.

  • See: wp_set_object_terms()
Works based on: wp_set_object_terms()

No Hooks.

Return

Array/false/WP_Error. Array of term taxonomy IDs of affected terms. WP_Error or false on failure.

Usage

wp_set_post_terms( $post_id, $tags, $taxonomy, $append );
$post_id(int)
The Post ID. Does not default to the ID of the global $post.
Default: 0
$tags(string/array)
An array of terms to set for the post, or a string of terms separated by commas.
Default: ''
$taxonomy(string)
Taxonomy name.
Default: 'post_tag'
$append(true/false)
If true, don't delete existing terms, just add on. If false, replace the terms with the new terms.
Default: false

Code of wp_set_post_terms: wp-includes/post.php VER 4.9.8

<?php
function wp_set_post_terms( $post_id = 0, $tags = '', $taxonomy = 'post_tag', $append = false ) {
	$post_id = (int) $post_id;

	if ( !$post_id )
		return false;

	if ( empty($tags) )
		$tags = array();

	if ( ! is_array( $tags ) ) {
		$comma = _x( ',', 'tag delimiter' );
		if ( ',' !== $comma )
			$tags = str_replace( $comma, ',', $tags );
		$tags = explode( ',', trim( $tags, " \n\t\r\0\x0B," ) );
	}

	/*
	 * Hierarchical taxonomies must always pass IDs rather than names so that
	 * children with the same names but different parents aren't confused.
	 */
	if ( is_taxonomy_hierarchical( $taxonomy ) ) {
		$tags = array_unique( array_map( 'intval', $tags ) );
	}

	return wp_set_object_terms( $post_id, $tags, $taxonomy, $append );
}

Related Functions

From tag: Term (taxonomies terms)

More from category: For posts

No comments
    Hello, !     Log In . Register