usa terms

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.

So here is how to automatically insert all US states in a WordPress taxonomy.

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);

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

Member since January 2, 2019

Fullstack Web Developer with more than 12 years of experience in web development. Adept in all stages of advanced web development. Knowledgeable in the user interface, backend, testing, and debugging processes. Bringing forth expertise in design, installation, testing, and maintenance of web systems. Working exclusively and professionally with WordPress since 2010.


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