How to automatically add tags to WordPress posts

This one drove me crazy, but I finally figured it out.

In WordPress, if you need to automatically add tags to a post via a PHP script, the best way to do it is via the wp_set_object_terms() function. There is no handy “wp_insert_tag()” function, or something like that, so look no further.


wp_set_object_terms() is a powerful function that not only helps you assign tags to posts (or pages), but also categories and other terms. For this example, here’s the PHP code needed to assign tags to a post.

   $tags = array('html', 'css', 'javascript');
   wp_set_object_terms( $post_id, $tags, 'post_tag', true );

The code above will create the new tags (or terms) if they don’t exist and link them to the post specified by $post_id.

For more info on this function and its parameter, check out the codex page at

Be Sociable, Share!


    1. Thanks for the tip, very useful !

    2. I think I this will be helpful on a project I’m working on.

      I’m looking for a way to automatically fetch the SEO meta keyword from The All in one SEO Plugin to add as a post tag. The idea is to make the meta keyword the same as the post tag upon hitting publish.

      I appreciate your help in figuring out how to implement the above snippet on the project I’m working with.


      • Hi Aris,

        The All-in-one SEO pack plugin for WP actually has an option to use post tags as meta keywords (in addition to any other keywords you may have already added to the post), possibly accomplishing what you are looking for. I’m not sure if it does it by default, but you can definitely choose to do so on the plugin’s option page.

        Please let me know if that is what you were looking for. Otherwise, I’d be happy to help you further explore the matter.


        • Thanks for your help, Vidal!

          I am aware of that feature of the All in one SEO plugin where you can automatically set the tags as meta keywords.

          But the solution I want to implement is the the other way around.

          I want the the keywords on SEO meta keywords to be automatically fetch or converted into post tags upon hitting publish..

          Thanks Again…

          Best Regards,

          • Hi Aris,

            Sorry it took me so long to get back to you, but I finally got some time to take a stab at this.

            I found a solution to your problem using jQuery. Paste the code below to your theme’s functions.php file and it will allow you to add all-in-one-seo-pack keywords as post tags on publish/update.

            Thanks, and please let me know how it goes!


            < ?php function aiosp_convert_keywords_to_tags(){?> jQuery(document).ready(function($){ $('form#post').submit(function(event){ $('#new-tag-post_tag').val($('input[name="aiosp_keywords"]').val()); }); }); <?php } add_action('admin_footer', 'aiosp_convert_keywords_to_tags'); ?>

            • Thanks for putting time on this, Vidal!

              I will let you know of the results once I implement your solution.

              I really appreciate your help..



            • Hi Vidal,

              Your above solutions works like charm when I implement it on my project.

              Thank you very much! More power…

              Kind Regards,


            • You are welcome! I’m glad it worked.

    3. Thanks for the tip, I was looking at edit_post with no luck at all, this did the trick

    4. I made a plugin that adds tags to posts automatically. It is called Automatic Post Tagger. Check it out, it is the best autotagging plugin. 🙂

    5. Hi Vidal,

      I tried your solution but it didn’t work for me :S Did I miss something!

      • Hi Amal,

        Sorry about the delay on my response. Can you show me the code you are trying to implement, so I can get more details? I think I may be able to help.


    6. Hi Vidal,

      Thank you for replying. Actually I was looking for a way to generate post tags automatically after publishing the post when I saw this conversation and your solution.
      I wasn’t using “All-in-one SEO pack plugin” before so I installed it and added the code you mentioned previously to the functions.php file of my theme but nothing changed.

      I’m not familiar with “All-in-one SEO pack” plugin, that’s why I asked if I missed something. knowing that I modified its settings to dynamically generate keywords for posts page.

      Any help in auto-generating tags would be very appreciated.


      • Hi Amal,

        Again, sorry about the delay… I didn’t get a notification on this response. We’ll keep a closer eye on this.

        Just to clarify: did you follow the instructions on the “WordPress: how to automatically convert custom fields to post tags” post? That one is a bit different than this post.

        The WordPress: how to automatically convert custom fields to post tags covers how to convert a certain custom fields to tags, which you need to specify in the $custom_field_names array. For example, if your custom field’s name is “test”, and you want its value to be added as a tag when you save the post, you need to specify it in the array:

        $custom_field_names = array('test');

        If you have more than one custom field you want to add as a tag (let’s say, “test” and “myothertest”), then the array would be:

        $custom_field_names = array( 'test', 'myothertest');

        Maybe that’s the part you are missing right now. Try it out, and please let me know if it works for you.




    1. […] while ago, someone asked something similar, and I put together a little script to help, but now I refined that script to be more encompassing. […]

    Leave a Comment