wp_set_post_terms() WordPress Function

The wp_set_post_terms() function is used to set the terms for a post. This function accepts an array of terms, each of which can be an id, name, or slug.

wp_set_post_terms( int $post_id, string|array $tags = '', string $taxonomy = 'post_tag', bool $append = false ) #

Set the terms for a post.


Description

Top ↑

See also


Top ↑

Parameters

$post_id

(int)(Optional) The Post ID. Does not default to the ID of the global $post.

$tags

(string|array)(Optional) An array of terms to set for the post, or a string of terms separated by commas. Hierarchical taxonomies must always pass IDs rather than names so that children with the same names but different parents aren't confused.

Default value: ''

$taxonomy

(string)(Optional) Taxonomy name.

Default value: 'post_tag'

$append

(bool)(Optional) If true, don't delete existing terms, just add on. If false, replace the terms with the new terms.

Default value: false


Top ↑

Return

(array|false|WP_Error) Array of term taxonomy IDs of affected terms. WP_Error or false on failure.


Top ↑

Source

File: wp-includes/post.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 );
}


Top ↑

Changelog

Changelog
VersionDescription
2.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More