Function Reference/wp set post terms
Description
Set terms for a post.
Uses wp_set_object_terms().
Usage
<?php wp_set_post_terms( $post_id, $terms, $taxonomy, $append ) ?>
Parameters
- $post_id
- (integer) (required) Post ID.
- Default: 0
- $terms
- (string,array) (optional) List of tags. Can be an array or a comma separated string. If you want to enter categories, then use ids. If you want to add tags, then use names.
- Default: array
- $taxonomy
- (string) (optional) Possible values for example: 'category', 'post_tag', 'taxonomy slug'
- Default: post_tag
- $append
- (boolean) (optional) If true, tags will be appended to the post. If false, tags will replace existing tags.
- Default: false
Return Values
- (array|boolean|WP_Error|string)
-
- (array) An array of the terms affected if successful,
- (boolean) false if integer value of $post_id evaluates as false (if ( ! (int) $post_id )),
- (WP_Error) The WordPress Error object on invalid taxonomy ('invalid_taxonomy').
- (string) The first offending term if a term given in the $terms parameter is named incorrectly. (Invalid term ids are accepted and inserted).
Examples
Notes
Hierarchical terms must always pass the id rather than the term name to avoid confusion where there may be another child with the same name.
To get the term id you can use:
$term_id = term_exists( $term, $taxonomy, $parent );
You may also need to pass by reference:
wp_set_post_terms( $post_id, $term, &$taxonomy );
Change Log
Since: 2.8.0
Source File
wp_set_post_terms() is located in wp-includes/post.php.
Related