Programmatically insert taxonomy terms in WordPress

I’ve got this dilemma today. I had to insert all states of America in DB as a taxonomy term. Instead of adding each name manually one by one I choose to do it automatically. Drop this code somewhere in your theme or plugin and run it only one time.

It will insert all United States of America in DB, and each of them will have the abbreviation as a slug. In my case, I needed this too, but you can choose to not do it by commenting on the third argument in wp_insert_term function.

Quick and easy way to insert taxonomy terms

We can quickly insert the terms and let WordPress do it under the hood by default however it knows best. Here is a simple example:

$terms = [ "Alaska", "Kansas", "New York" ];

foreach ($terms as $term) {
    wp_insert_term($term, $taxonomyName);
}

Programatically insert taxonomy terms and define the slugs

In my case, the slugs are the state abbreviations and they play an important role, so here is how to automatically insert all US states in a WordPress taxonomy.

<?php 
add_action('init', function () {
    $taxonomyName = 'state';

    $states = [
        "AK" => "Alaska",
        "AL" => "Alabama",
        "AR" => "Arkansas",
        "AS" => "American Samoa",
        "AZ" => "Arizona",
        "CA" => "California",
        "CO" => "Colorado",
        "CT" => "Connecticut",
        "DC" => "District of Columbia",
        "DE" => "Delaware",
        "FL" => "Florida",
        "GA" => "Georgia",
        "GU" => "Guam",
        "HI" => "Hawaii",
        "IA" => "Iowa",
        "ID" => "Idaho",
        "IL" => "Illinois",
        "IN" => "Indiana",
        "KS" => "Kansas",
        "KY" => "Kentucky",
        "LA" => "Louisiana",
        "MA" => "Massachusetts",
        "MD" => "Maryland",
        "ME" => "Maine",
        "MI" => "Michigan",
        "MN" => "Minnesota",
        "MO" => "Missouri",
        "MS" => "Mississippi",
        "MT" => "Montana",
        "NC" => "North Carolina",
        "ND" => "North Dakota",
        "NE" => "Nebraska",
        "NH" => "New Hampshire",
        "NJ" => "New Jersey",
        "NM" => "New Mexico",
        "NV" => "Nevada",
        "NY" => "New York",
        "OH" => "Ohio",
        "OK" => "Oklahoma",
        "OR" => "Oregon",
        "PA" => "Pennsylvania",
        "PR" => "Puerto Rico",
        "RI" => "Rhode Island",
        "SC" => "South Carolina",
        "SD" => "South Dakota",
        "TN" => "Tennessee",
        "TX" => "Texas",
        "UT" => "Utah",
        "VA" => "Virginia",
        "VI" => "Virgin Islands",
        "VT" => "Vermont",
        "WA" => "Washington",
        "WI" => "Wisconsin",
        "WV" => "West Virginia",
        "WY" => "Wyoming",
    ];
    foreach ($states as $slug => $name) {
        wp_insert_term($name, $taxonomyName, [
            'slug' => $slug,
        ]);
    }
}, 999);

Note: This method can be used to insert any taxonomy terms. Just edit the array and the taxonomy name.

Check if the term exists before adding it:

Some terms may already be in DB. So it’s a good idea to check this. Verify if that’s true and insert only the missing ones.

foreach ($states as $slug => $name) {
    if (empty(term_exists($slug, $taxonomyName))) {
        wp_insert_term($name, $taxonomyName, [
            'slug' => $slug,
        ]);
    }
}
Member since January 2, 2019

As a seasoned WordPress developer with expertise in various tech stacks and languages, I bring years of experience to every project I handle. My passion for coding and dedication to delivering exceptional work ensures that each project I take on is of the highest quality. I specialize in creating custom themes, developing plugins, and building full-scale web systems. By staying up-to-date with the latest industry trends and best practices, I incorporate cutting-edge solutions into my work.

Comments

  • Paul C 1 year ago

    Thanks for taking the time to write this article. I needed to know how to insert taxonomy terms programmatically and this is the first thing that came up on a search. Answers my question well.

    One thing I’m wondering is if the conditional “if empty term_exists…” should be inside the foreach loop instead of the foreach loop being inside the conditional.

    Thanks!

  • Andrei 1 year ago

    Yes, you’re right. I’ve put the conditional check outside of the loop by mistake. I’ve updated the article. Thank you for pointing this up.

    Anyway, glad to see that the article helped you 🙂

  • NeaMitika 7 months ago

    Multumesc! 🙂

Cancel reply

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