wp_set_post_terms() – Set the terms for a post.

You appear to be a bot. Output may be restricted

Description

Set the terms for a post.

Usage

$array|false|WP_Error = wp_set_post_terms( $post_id, $tags, $taxonomy, $append );

Parameters

$post_id
( int ) optional – Optional. The Post ID. Does not default to the ID of the global $post.
$tags
( string|array ) optional – 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 empty.
$taxonomy
( string ) optional default: post_tag – Optional. Taxonomy name. Default 'post_tag'.
$append
( bool ) optional – Optional. If true, don't delete existing terms, just add on. If false, replace the terms with the new terms. Default false.

Returns

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

Source

File name: wordpress/wp-includes/post.php
Lines:

1 to 29 of 29
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 );
}
 

 View on GitHub View on Trac