Created
July 29, 2024 09:05
-
-
Save joviczarko/3c0482235bae1a8aac1b8d276eebdf90 to your computer and use it in GitHub Desktop.
Importing countries in taxonomy hierarchically per region/continent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Intended to run via WP-CLI using 'wp eval-file import-taxonomy-terms.php' command | |
*/ | |
// Ensure WordPress is loaded | |
if ( ! defined('WPINC') ) { | |
die; | |
} | |
function insert_country_terms() { | |
$taxonomy = 'country'; // Your custom taxonomy slug | |
$countries_hierarchy = array( | |
'Africa' => array( | |
'Algeria', | |
'Angola', | |
'Benin', | |
'Botswana', | |
'Burkina Faso', | |
'Burundi', | |
'Cabo Verde', | |
'Cameroon', | |
'Central African Republic', | |
'Chad', | |
'Comoros', | |
'Congo', | |
'Djibouti', | |
'Egypt', | |
'Equatorial Guinea', | |
'Eritrea', | |
'Eswatini', | |
'Ethiopia', | |
'Gabon', | |
'Gambia', | |
'Ghana', | |
'Guinea', | |
'Guinea-Bissau', | |
'Ivory Coast', | |
'Kenya', | |
'Lesotho', | |
'Liberia', | |
'Libya', | |
'Madagascar', | |
'Malawi', | |
'Mali', | |
'Mauritania', | |
'Mauritius', | |
'Morocco', | |
'Mozambique', | |
'Namibia', | |
'Niger', | |
'Nigeria', | |
'Rwanda', | |
'Sao Tome and Principe', | |
'Senegal', | |
'Seychelles', | |
'Sierra Leone', | |
'Somalia', | |
'South Africa', | |
'South Sudan', | |
'Sudan', | |
'Tanzania', | |
'Togo', | |
'Tunisia', | |
'Uganda', | |
'Zambia', | |
'Zimbabwe' | |
), | |
'Asia' => array( | |
'Afghanistan', | |
'Armenia', | |
'Azerbaijan', | |
'Bahrain', | |
'Bangladesh', | |
'Bhutan', | |
'Brunei', | |
'Cambodia', | |
'China', | |
'Cyprus', | |
'Georgia', | |
'India', | |
'Indonesia', | |
'Iran', | |
'Iraq', | |
'Israel', | |
'Japan', | |
'Jordan', | |
'Kazakhstan', | |
'Kuwait', | |
'Kyrgyzstan', | |
'Laos', | |
'Lebanon', | |
'Malaysia', | |
'Maldives', | |
'Mongolia', | |
'Myanmar', | |
'Nepal', | |
'North Korea', | |
'Oman', | |
'Pakistan', | |
'Palestine', | |
'Philippines', | |
'Qatar', | |
'Saudi Arabia', | |
'Singapore', | |
'South Korea', | |
'Sri Lanka', | |
'Syria', | |
'Taiwan', | |
'Tajikistan', | |
'Thailand', | |
'Timor-Leste', | |
'Turkey', | |
'Turkmenistan', | |
'United Arab Emirates', | |
'Uzbekistan', | |
'Vietnam', | |
'Yemen' | |
), | |
'Europe' => array( | |
'Albania', | |
'Andorra', | |
'Armenia', | |
'Austria', | |
'Azerbaijan', | |
'Belarus', | |
'Belgium', | |
'Bosnia and Herzegovina', | |
'Bulgaria', | |
'Croatia', | |
'Cyprus', | |
'Czech Republic', | |
'Denmark', | |
'Estonia', | |
'Finland', | |
'France', | |
'Georgia', | |
'Germany', | |
'Greece', | |
'Hungary', | |
'Iceland', | |
'Ireland', | |
'Italy', | |
'Kazakhstan', | |
'Kosovo', | |
'Latvia', | |
'Liechtenstein', | |
'Lithuania', | |
'Luxembourg', | |
'Malta', | |
'Moldova', | |
'Monaco', | |
'Montenegro', | |
'Netherlands', | |
'North Macedonia', | |
'Norway', | |
'Poland', | |
'Portugal', | |
'Romania', | |
'Russia', | |
'San Marino', | |
'Serbia', | |
'Slovakia', | |
'Slovenia', | |
'Spain', | |
'Sweden', | |
'Switzerland', | |
'Turkey', | |
'Ukraine', | |
'United Kingdom', | |
'Vatican City' | |
), | |
'North America' => array( | |
'Antigua and Barbuda', | |
'Bahamas', | |
'Barbados', | |
'Belize', | |
'Canada', | |
'Costa Rica', | |
'Cuba', | |
'Dominica', | |
'Dominican Republic', | |
'El Salvador', | |
'Grenada', | |
'Guatemala', | |
'Haiti', | |
'Honduras', | |
'Jamaica', | |
'Mexico', | |
'Nicaragua', | |
'Panama', | |
'Saint Kitts and Nevis', | |
'Saint Lucia', | |
'Saint Vincent and the Grenadines', | |
'Trinidad and Tobago', | |
'United States' | |
), | |
'Oceania' => array( | |
'Australia', | |
'Fiji', | |
'Kiribati', | |
'Marshall Islands', | |
'Micronesia', | |
'Nauru', | |
'New Zealand', | |
'Palau', | |
'Papua New Guinea', | |
'Samoa', | |
'Solomon Islands', | |
'Tonga', | |
'Tuvalu', | |
'Vanuatu' | |
), | |
'South America' => array( | |
'Argentina', | |
'Bolivia', | |
'Brazil', | |
'Chile', | |
'Colombia', | |
'Ecuador', | |
'Guyana', | |
'Paraguay', | |
'Peru', | |
'Suriname', | |
'Uruguay', | |
'Venezuela' | |
) | |
); | |
foreach ($countries_hierarchy as $region => $countries) { | |
// Insert the region term | |
if (!term_exists($region, $taxonomy)) { | |
$region_term = wp_insert_term($region, $taxonomy); | |
} else { | |
$region_term = get_term_by('name', $region, $taxonomy); | |
} | |
if (is_wp_error($region_term)) { | |
echo 'Failed to insert region: ' . $region . "\n"; | |
continue; | |
} | |
$region_term_id = is_wp_error($region_term) ? $region_term->get_error_data() : $region_term['term_id']; | |
// Insert the country terms | |
foreach ($countries as $country) { | |
if (!term_exists($country, $taxonomy)) { | |
wp_insert_term($country, $taxonomy, array('parent' => $region_term_id)); | |
} | |
} | |
} | |
echo 'Terms have been imported successfully.' . "\n"; | |
} | |
insert_country_terms(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment