WordPress : How to Set a tag by default

I have a question. I want to add a tag automatically on my posts but I don’t know how to do it .

I tried to use : wp_set_post_tags() but nothing.

Read More

Help please. Thanks in advance.

Related posts

Leave a Reply

3 comments

  1. Spent a hot minute looking for a solution to this same problem. Just found it by combining a few solutions to semi-related requests, thought I’d share. You are the right track with wp_set_post_tags() but the key is to set the tag on publish so that an ID can be passed. The following code will add the tag “archive” once published. Note that I’m using a custom post type of “andytoday”, you’ll need to change that to “post” for standard usage or your specific custom post type name if applicable. Add this to functions.php and rename the function accordingly. Dont for get to rename in the hook as well.

    function set_archive_tag_on_publish($post_id,$post) {
      if ($post->post_type == 'andytoday'
        && $post->post_status == 'publish') {
          wp_set_post_tags( $post_id, 'archive', true );
        }
      }
    add_action('save_post','set_archive_tag_on_publish',10,2);
    
  2. THE SOLUTION

    By using wp_set_object_terms()

    function tag() {
    global $post;
    $tags = array(‘Hey’, date(‘Y’), ‘Cool’);
    wp_set_object_terms($post->ID, $tags,’post_tag’, true );
    }

    add_action(‘publish_page’, ‘tag’);