Automatically Add Categories to Posts Based on Tags

The code below will automatically add categories to a post when it is published or updated.  This operates on one tag at a time and adds a category each time it finds a tag in the post that matches a tag in the $tag_categories array. So, if a post is in ‘info’, it will add category ‘information’, etc. Since it loops through all the tags for a post, it might add several categories.

You will need to modify the $tag_categories array.

   // Add a category to a post for each of selected tags the post already has.
   // Modify the $tag_categories array.  Use one tag name => one category name
   function mam_add_category ($post_id = 0) {
     if (!$post_id) return;
     $tag_categories = array ( // The tag names and the corresponding category name to add
       'info' => 'information',
       'INFO' => 'information',
          'search optimization' => 'SEO',
     );
     $post_tags = get_the_tags($post_id);
     foreach ($post_tags as $tag) {
       if ($tag_categories[$tag->name] ) {
         $cat_id = get_cat_ID($tag_categories[$tag->name]);
         if ($cat_id) {
           $result =  wp_set_post_terms( $post_id, $tags = $cat_id, $taxonomy = 'category', $append = true );
         }
       }
     }
   }
add_action('publish_post','mam_add_category');
?>

17 Responses to Automatically Add Categories to Posts Based on Tags

  • lev says:

    hi, ive implemented you code, but every time it adds UNCATEGORIZED category and tag as well, how can i fix that?

    • Mac McDonald says:

      The code does not add any tags, and only the categories corresponding to the tag names you put in $tag_categories. The ‘Uncategorized’ values are probably there by default. It would require an entirely different function to remove them.

  • BP says:

    I am extremely new to wordpress (built my site yesterday), but not coding, hosting etc.

    Which functions.php should this be inserted into? I tried placing it into wp-include which broke my site for a moment or two until the backup was uploaded 🙂

    I am currently using multi rss importer which tags the posts as they flow in and a theme which automatically adds categories to the menu bar. This function should solve the gap between the two perfectly I hope!

    Thanks in advance.

    • Mac McDonald says:

      You should create a Child theme to make your changes. If you do not, all of your changes will be lost if you update your theme.

      The code should go in the functions.php file in your child theme. But, be careful. If you cause an error in functions.php, your site will be unusable and you must use either ftp or a file manager from your hosting service to correct it.

  • Jim says:

    Can someone tell me which is the file I must put this code ?

    Thanks a lot !

    • Mac McDonald says:

      The code is a function which goes in functions.php. However, you need to be careful when changing functions.php. If you cause an error, your site will be unusable until you correct the problem using ftp.

      Did you see the comment below by Michael regarding the Autotagger plugin? This might be better for your use.

  • Michael says:

    Thanks for the info Mac.

    I found a WordPress plugin – Autotagger – which automatically converts categories to tags. It does other things too, but this is the only one I needed.

    So … problem solved and not a lot of effort expended! 🙂

  • Steve says:

    Can you please suggest where the $tag_categories array is located ?

    • Mac McDonald says:

      That array is created in lines 5 – 9 of the code above.

      You enter the tag names as keys with the category names as values. So in the example code above, a tag name of ‘INFO’ will create a category of ‘information’.

  • Steve says:

    Hey this is exactly what I need. I assume the category assignment happens at post creating time ? Can you please if this is likely still valid for WordPress 3.4.2 ?

  • Dave says:

    Hi, like your site.
    That’s almost what i’m searching for.
    I want Automatically Add Tags to Posts Based on Categories.
    is there a way to do this? what i want is when i click the category the tags will show up automatically.
    Thanks.

    • Michael says:

      Hi Dave

      Did you work out how to do this? Is it as simple (hic!) as reversing this code so that tags = categories and categories = tags?

      I need to do the same as you – the first part anyway – and I’m looking for an ‘easy’ method if one exists, instead of reinventing the wheel!

      • Mac McDonald says:

        Hi Michael,

        I think the only problem you will have is that WP does not have a get_tag_ID() function that corresponds to get_cat_ID(). You would have to write your own.

Leave a Reply

Your email address will not be published. Required fields are marked *

*