I have a simple text file with a list of words (Berlin Hamburg Stuttgart ...) and so on. They are cities of germany an nearly 2000 counted. I need everyone of them as a category in my wordpress site and looking for the best way to achieve this. Is there a faster way than to create every single category manually from the dashboard?

Thanks for your help!

up vote 0 down vote accepted

Assuming you have a nice list of your Words, you could easily create an array with them, doing a little data manipulation.

Afterwards you can loop throug your $array and insert the Term accordingly:

$yourcities = file($filename, FILE_IGNORE_NEW_LINES); // all your valuesfrom the .txt file

foreach( $yourcities as $thiscity ) {

    wp_insert_term(
        $thiscity, // the term, in your case the name of the city
        'category', // the taxonomy, in your case category
        array(
            'description'=> '', // if available
            'slug' => '', // leave blank if not available
            'parent'=> $parent_term_id // if you do it hierarchical, be sure to insert the parent ID here
        )
    );

}
  • Thank you for your suggestion. But how do I have to do it? I have just a text-document with a list of words. What should I exactly do with it? – user45836 Jan 29 '14 at 11:14
  • Try doing it like this. I updated the answer to include this method :) – fischi Jan 29 '14 at 11:15
  • Okay and where do I have to place the code exactly and store the txt. file? – user45836 Jan 29 '14 at 11:19
  • You can store the code in your functions.php or create a plugin for it. The easiest way would be to upload the .txt into the same folder. Be sure to execute the code only once! – fischi Jan 29 '14 at 11:23
  • 2 more things: It's not the default category for posts, it's the taxonomy=ad_location from the posttype=ad. The text-file is calles staedte.txt and located now in the same folder like the function.php. How does the code looks now for me? – user45836 Jan 29 '14 at 11:45

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.