I want to add a specific tag to posts being published, I tried the following code and searched for answers here but it doesn't work. Does anyone have an idea why?

function tag_to_published_post( $post_id, $post, $update ) {
// check if just an update
if ( $update ) {
//add tag
wp_set_post_tags( $post_id, 'tag-to-be-added', true ); 
add_action( 'save_post', 'tag_to_published_post', 10, 3);

Thanks for any ideas!

  • 1
    Please define "doesn't work".
    – vancoder
    May 26 '21 at 17:34
  • 1
    On my wp this code works. It adds tag `'tag-to-be-added' even before post save this tag is assigned to post.
    – anton
    May 26 '21 at 20:13
  • Ah, yeah, thanks, you're right, seems like it works from the backend. I failed to mention that I tried using it with a front end post submission (the Plugin). Is there any way to make it work under these circumstances?
    – fourorty
    May 27 '21 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.