Ask your WordPress questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

Pre-populate a hierarchical taxonomy - upto four levels WordPress

  • SOLVED

Hello,

Im trying to pre-populate a custom taxonomy with regions, but am having trouble handling the multi dimensional array required for a hierarchical category.

At the moment I have a simple plugin that creates the taxonomy (loosely based on: https://github.com/curtismchale/WP-Theme-Tutorial---US-States-Plugin-for-WordPress)

The listing I would like to create looks something like:

- US
-- North East
--- State
--- State
--- State
- North West
--- State
--- State
--- State
- International
-- North America
--- Canada
--- Mexico
-- Europe
--- UK
---- England
---- Scotland
---- Wales
--- Country
--- Country

Requirements:
If your code it was built upon a plugin like the git example, that would make it easier for me to test.

An example array that could generate the above example
A function that iterates over this array and
- Check that the entry does not already exist
- Add the entries to the taxonomy under the appropriate parent

Also, multidimensional arrays can be tricky (which is why I'm here) so if you want to recommend a different approach im open as long as it is easy to read and and concise.

Inspiration:
I've been looking at the following plugin example for a flat list (git example post):
http://wpthemetutorial.com/2012/08/16/prepoulating-taxonomies-wordpress/

And have noted this SE topic about a WP caching bug when inputting a hierarchical taxonomy
http://wordpress.stackexchange.com/questions/8357/inserting-terms-in-an-hierarchical-taxonomy

Answers (1)

2014-07-19

Bob answers:

I have modified a plugin please check it.

[[LINK href="http://wp-lovers.com/wpquestions/states-texonomy.zip"]]http://wp-lovers.com/wpquestions/states-texonomy.zip[[/LINK]]

Please note you should first delete old texonomy created by the old plugin.
Let me know if you have any trouble.

Regards,
Bob


Bob comments:

Attaching image of output of above plugin.