wp_set_post_tags()
Sets tags for a post.
No Hooks.
Returns
Array|false|WP_Error
.
false if $post_id is not an int or equals 0, otherwise - an array of IDs that were involved or a WP_Error object.
Usage
wp_set_post_tags( $post_id, $tags, $append );
- $post_id(integer) (required)
- ID of the post. By default, does NOT refer to the global variable global $post.
- $tags(string/array)
List of taxonomy items as an array or as a comma-separated string.
The function will create new tags if the specified ones are not found. If a name is specified (in Cyrillic), the function will create a tag. In this case: the name will be the name, the slug will be the slug (processed as usual).
If an ID or an array of IDs is passed, make sure that the variables are passed as integers, not as strings! Because strings that look like integers, for example, '98' will be interpreted as the term name, not its ID!
Default: ''
- $append(boolean)
- Append the specified tags to the post or replace them.
If true, the current relationship of the post with the tags will not be broken, and the new specified tags will be added to the current ones.
If false, only the specified tags will be set for the post, and all other tags will be removed.
Default: false
Examples
#1 Add tags for the post
This example adds post 42 to the "feelings" and "life" tags (current post tags remains). If the specified tags do not exist, they will be created:
wp_set_post_tags( 42, 'feelings,life', true );
#2 Add array of tags to post with ID 100
Current post tags will be unset and specified ones will be set.
$post_id = 100; $tags = [ 'Mango', 'Apple', 'Banana' ]; wp_set_post_tags( $post_id, $tags );
Notes
Changelog
Since 2.3.0 | Introduced. |
wp_set_post_tags() wp set post tags code WP 6.8.1
function wp_set_post_tags( $post_id = 0, $tags = '', $append = false ) { return wp_set_post_terms( $post_id, $tags, 'post_tag', $append ); }