On this page
wp_set_post_terms( int $post_id, string|array $terms = '', string $taxonomy = 'post_tag', bool $append = false ): array|false|WP_Error
Sets the terms for a post.
Description
See also
Parameters
$post_idint Optional-
The Post ID. Does not default to the ID of the global $post.
$termsstring|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:
'' $taxonomystring Optional-
Taxonomy name. Default
'post_tag'.Default:
'post_tag' $appendbool Optional-
If true, don't delete existing terms, just add on. If false, replace the terms with the new terms.
Default:
false
Return
array|false|WP_Error Array of term taxonomy IDs of affected terms. WP_Error or false on failure.
Source
File: wp-includes/post.php. View all references
function wp_set_post_terms( $post_id = 0, $terms = '', $taxonomy = 'post_tag', $append = false ) {
$post_id = (int) $post_id;
if ( ! $post_id ) {
return false;
}
if ( empty( $terms ) ) {
$terms = array();
}
if ( ! is_array( $terms ) ) {
$comma = _x( ',', 'tag delimiter' );
if ( ',' !== $comma ) {
$terms = str_replace( $comma, ',', $terms );
}
$terms = explode( ',', trim( $terms, " \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 ) ) {
$terms = array_unique( array_map( 'intval', $terms ) );
}
return wp_set_object_terms( $post_id, $terms, $taxonomy, $append );
}
Related
Uses
| Uses | Description |
|---|---|
| is_taxonomy_hierarchical() wp-includes/taxonomy.php | Determines whether the taxonomy object is hierarchical. |
| wp_set_object_terms() wp-includes/taxonomy.php | Creates term and taxonomy relationships. |
| _x() wp-includes/l10n.php | Retrieves translated string with gettext context. |
Used By
| Used By | Description |
|---|---|
| wp_set_unique_slug_on_create_template_part() wp-includes/theme-templates.php | Sets a custom slug when creating auto-draft template parts. |
| wp_publish_post() wp-includes/post.php | Publishes a post by transitioning the post status. |
| wp_set_post_tags() wp-includes/post.php | Sets the tags for a post. |
| wp_set_post_categories() wp-includes/post.php | Sets categories for a post. |
| wp_insert_post() wp-includes/post.php | Inserts or update a post. |
| set_post_format() wp-includes/post-formats.php | Assign a format to a post |
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/wp_set_post_terms