WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp set post tags

Description

Set tags for a post. Every tag that does not already exist will be automatically created. Uses wp_set_post_terms().

Usage

<?php wp_set_post_tags$post_ID$tags$append ?>

Parameters

$post_ID
(integer) (required) Post ID.
Default: 0
$tags
(string,array) (optional) List of tags. Can be an array or a comma separated string.
Default: array
$append
(boolean) (optional) If true, tags will be appended to the post. If false, tags will replace existing tags.
Default: false

Return Values

(boolean|null) 

Will return false if $post_id is not an integer or is 0. Will return null otherwise.

Examples

To add the tags meaning and life to the current tags of the post with ID 42:

wp_set_post_tags( 42, 'meaning,life', true );

Notes

  • If you set IDs of an existing tag in the array, WorPress assigns the existing tag.
  • If you pass text in the array, WP will create a tag if it doesn't exist and assignes it to the post
  • You can mix text and IDs. The text will create a term, if it not exists, the ID will be used for an existing tag - both get assigned to the post.

Hooks

Change Log

Since: 2.3.0

Source File

wp_set_post_tags() is located in wp-includes/post.php.

Related

wp_insert_term wp_delete_term wp_set_post_terms

See also index of Function Reference and index of Template Tags.