1

I have imported cities as term named as sf-cities I'm trying to add countries name as term meta I have attached sample of my code.

add_action( 'event_notification', 'import_countries', 10, 0 );
function import_countries(){
    $response = wp_remote_get('https://raw.githubusercontent.com/russ666/all-countries-and-cities-json/master/countries.json');
    $response_body = wp_remote_retrieve_body( $response );
    $countries = json_decode($response_body);
    
    $last_id = 0;
    
    $terms = get_terms( array(
        'taxonomy' => 'sf-cities',
         'hide_empty' => false,
    )); 
    $count = 1;
    foreach($countries as $country => $cities) {
        foreach($cities as $city) {
            $term = get_term_by('name', $city, 'sf-cities');
            if( $count == 1000 ) {
                $count = 1;
                usleep(500);
            }
            if(!empty( $term )) {
                if( $term->term_id > 220 ) { 
                    if( empty( get_term_meta( $term->term_id , 'country', true) )) {
                        add_term_meta( $term->term_id ,'country', $country );       
                    } else {
                        update_term_meta( $term->term_id ,'country', $country );        
                    }
                    
                } 
                $last_id = $term->term_id;
            } else {
                continue;
            }
            $count++;
        }
    }
    return $last_id;
}

Even it was working in the json file there are 80k records in there. Is there any other possibility to add term meta value without cron job.

4
  • Why do you have to import it using a cron job? It's better to import it in a custom plugin. So that when you activate the plugin, these will be imported, and if you uninstall the plugin, these will be removed.
    – Scott
    Commented Feb 13, 2023 at 0:55
  • For updates, you may just implemnet an update button within the Plugin to re-run the process.
    – Scott
    Commented Feb 13, 2023 at 0:57
  • I am using service finder theme in this theme they are using the terms for storing city and country value. Thats why I create cron job for import the content.
    – Stephen
    Commented Feb 13, 2023 at 5:51
  • here is the link for the theme I have using. themeforest.net/item/…
    – Stephen
    Commented Feb 13, 2023 at 5:54

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.