Best way to import hierarchical taxonomy from data source?

[ad_1]

I am looking for some help with importing a hierarchical taxonomy into my site. I’ve created my own custom taxonomy and will be accessing the data source through a single API call. The returned data is a flat JSON response that has relationships built in based off their own ID’s from that data source.

Here is an example of the data:

{
“results”: [
{
“id”: 1,
“parent_id”: null,
“name”: “Technical”
},
{
“id”: 3,
“parent_id”: 1,
“name”: “Complete”
},
{
“id”: 4,
“parent_id”: 1,
“name”: “Expert”
},
{
“id”: 16,
“parent_id”: 1,
“name”: “Robots”
},
{
“id”: 17,
“parent_id”: 1,
“name”: “Camping”
}
] }

I started down the path of something like this:

foreach( $results as $result ) {
if ( !empty($result[‘parent_id’]) ) {
$term = wp_insert_term($result[‘name’], ‘custom_tax’);
//Custom ACF Field for the Term to capture API ID
update_field( ‘api_id’, $result[‘id’], $term[‘term_id’] );
}
}

Now, I am unsure of how I should possibly create a second loop to capture all of the `results` with a `parent_id` and how to set that parent id relationship with my taxonomy since it will have a different term id in my WordPress database.

What’s the best way for me to import the data from the API and create my own hierarchy in my taxonomy?

[ad_2]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer