简体   繁体   中英

Programmatically Add Tag to Wordpress Post Without Overwriting Pre-Existing Tags

I am able to add a tag to my post in code, but it overwrites the existing array of tags. Does anyone know of a way to add a tag to the array of pre-existing tags so that none are erased?

wp_set_object_terms($post_id, 'mytagname', 'post_tag');

This part of the Wordpress API seems a bit clunky, but maybe I'm completely missing something. Any help would be appreciated!

Turns out RTFM was the answer.

Look at the fourth parameter of the function - $append . If it is set to true , the tags are appended to the existing tags, if it's false (the default) they replace them.

So you want to call it like so:

wp_set_object_terms($post_id, 'mytagname', 'post_tag', true);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM