Creating WordPress Taxonomy Terms WP-CLI with Parent and Child Relationships

Using Taxonomies and hierarchies is a great way to categorize your data in WordPress. I was curious how it would be possible to automate the creation of taxonomy terms with parent terms using a WP-CLI script. This tutorial will show you how to automatically re-create your taxonomy structure with child and parent terms using WP-CLI and bash scripting.

Creating WordPress Taxonomy Terms WP-CLI with Parent and Child Relationships

Here is a sample Taxonomy hierarchy I created for a client on Codeable for a food sensitivity site.

wp taxonomy list

Here is the output, we are going to focus on the fodmap_type term for this tutorial.

----------------+------------------+-------------+---------------+---------------+--------------+--------+
| name           | label            | description | object_type   | show_tagcloud | hierarchical | public |
+----------------+------------------+-------------+---------------+---------------+--------------+--------+
| category       | Categories       |             | post          | 1             | 1            | 1      |
| post_tag       | Tags             |             | post          | 1             |              | 1      |
| nav_menu       | Navigation Menus |             | nav_menu_item |               |              |        |
| link_category  | Link Categories  |             | link          | 1             |              |        |
| post_format    | Formats          |             | post          |               |              | 1      |
| food_type      | Food Types       |             | food          | 1             |              | 1      |
| lectin_level   | Lectins          |             | food          | 1             |              | 1      |
| paleo_aip      | Paleo AIPs       |             | food          | 1             |              | 1      |
| fodmap_overall | FODMAP Levels    |             | food          | 1             |              | 1      |
| food_state     | Food States      |             | food          | 1             |              | 1      |
| fodmap_type    | FODMAP Types     |             | food          | 1             | 1            | 1      |
+----------------+------------------+-------------+---------------+---------------+--------------+--------+

Here is the command for fetching the specific terms for a taxonomy, here fodmap_type

wp term list fodmap_type

This is the ASCII table version of the partial screenshot from the beginning.

+---------+------------------+----------+-----------------+-------------------------------------+--------+-------+
| term_id | term_taxonomy_id | name     | slug            | description                         | parent | count |
+---------+------------------+----------+-----------------+-------------------------------------+--------+-------+
| 99      | 99               | Fructan  | fructan         | Food's FODMAP Fructan level         | 0      | 285   |
| 103     | 103              | Fructose | fructose        | Food's FODMAP Fructose level        | 0      | 285   |
| 119     | 119              | GOS      | gos             | Food's FODMAP GOS level             | 0      | 285   |
| 102     | 102              | High     | high-fructan    | Food's FODMAP high Fructan level    | 99     | 28    |
| 106     | 106              | High     | high-fructose   | Food's FODMAP high Fructose level   | 103    | 13    |
| 110     | 110              | High     | high-lactose    | Food's FODMAP high Lactose level    | 107    | 0     |
| 114     | 114              | High     | high-mannitol   | Food's FODMAP high Mannitol level   | 111    | 11    |
| 118     | 118              | High     | high-sorbitol   | Food's FODMAP high Sorbitol level   | 115    | 20    |
| 122     | 122              | High     | high-gos        | Food's FODMAP high GOS level        | 119    | 10    |
| 107     | 107              | Lactose  | lactose         | Food's FODMAP Lactose level         | 0      | 285   |
| 100     | 100              | Low      | low-fructan     | Food's FODMAP low Fructan level     | 99     | 223   |
| 104     | 104              | Low      | low-fructose    | Food's FODMAP low Fructose level    | 103    | 260   |
| 108     | 108              | Low      | low-lactose     | Food's FODMAP low Lactose level     | 107    | 285   |
| 112     | 112              | Low      | low-mannitol    | Food's FODMAP low Mannitol level    | 111    | 255   |
| 116     | 116              | Low      | low-sorbitol    | Food's FODMAP low Sorbitol level    | 115    | 247   |
| 120     | 120              | Low      | low-gos         | Food's FODMAP low GOS level         | 119    | 262   |
| 111     | 111              | Mannitol | mannitol        | Food's FODMAP Mannitol level        | 0      | 285   |
| 101     | 101              | Medium   | medium-fructan  | Food's FODMAP medium Fructan level  | 99     | 34    |
| 105     | 105              | Medium   | medium-fructose | Food's FODMAP medium Fructose level | 103    | 12    |
| 109     | 109              | Medium   | medium-lactose  | Food's FODMAP medium Lactose level  | 107    | 0     |
| 113     | 113              | Medium   | medium-mannitol | Food's FODMAP medium Mannitol level | 111    | 19    |
| 117     | 117              | Medium   | medium-sorbitol | Food's FODMAP medium Sorbitol level | 115    | 18    |
| 121     | 121              | Medium   | medium-gos      | Food's FODMAP medium GOS level      | 119    | 13    |
| 115     | 115              | Sorbitol | sorbitol        | Food's FODMAP Sorbitol level        | 0      | 285   |
+---------+------------------+----------+-----------------+-------------------------------------+--------+-------+

In order to recreate this taxonomy term structure with parent relationships we can utilize WP-CLI and bash.

To start we need to create the parent term, let’s start with Fructan. We need to get the term ID so that we can specify this ID for the child terms under Fructan. WP-CLI makes this possible via the --porcelain flag.

With bash, we can use command substitution e.g. $() to capture the new taxonomy term ID in a variable so we can use it in the follow up commands for the child terms.

FRUCTAN=$(wp term create fodmap_type Fructan --description="Food's FODMAP Fructan level" --slug=fructan --porcelain)

Now we can create the child term underneatn Fructan and specify the parent term ID using the variable we created in the previous command ${FRUCTAN}

wp term create fodmap_type Low --description="Food's FODMAP low Fructan level" --slug=low-fructan --parent=${FRUCTAN}

This process can be repeated for the other child terms as well

wp term create fodmap_type Medium --description="Food's FODMAP medium Fructan level" --slug=medium-fructan --parent=${FRUCTAN}
wp term create fodmap_type High --description="Food's FODMAP high Fructan level" --slug=high-fructan --parent=${FRUCTAN}

You can also create a loop as well to make it more sophisticated, I have not done this just to make it easier to read.

Here is the entire script used to automatically re-create the taxonomy structure with parent relationships and child terms using WP-CLI

# Fructans
FRUCTAN=$(wp term create fodmap_type Fructan --description="Food's FODMAP Fructan level" --slug=fructan --porcelain)
wp term create fodmap_type Low --description="Food's FODMAP low Fructan level" --slug=low-fructan --parent=${FRUCTAN}
wp term create fodmap_type Medium --description="Food's FODMAP medium Fructan level" --slug=medium-fructan --parent=${FRUCTAN}
wp term create fodmap_type High --description="Food's FODMAP high Fructan level" --slug=high-fructan --parent=${FRUCTAN}

# Fructose
FRUCTOSE=$(wp term create fodmap_type Fructose --description="Food's FODMAP Fructose level" --slug=fructose --porcelain)
wp term create fodmap_type Low --description="Food's FODMAP low Fructose level" --slug=low-fructose --parent=${FRUCTOSE}
wp term create fodmap_type Medium --description="Food's FODMAP medium Fructose level" --slug=medium-fructose --parent=${FRUCTOSE}
wp term create fodmap_type High --description="Food's FODMAP high Fructose level" --slug=high-fructose --parent=${FRUCTOSE}

# Lactose
LACTOSE=$(wp term create fodmap_type Lactose --description="Food's FODMAP Lactose level" --slug=lactose --porcelain)
wp term create fodmap_type Low --description="Food's FODMAP low Lactose level" --slug=low-lactose --parent=${LACTOSE}
wp term create fodmap_type Medium --description="Food's FODMAP medium Lactose level" --slug=medium-lactose --parent=${LACTOSE}
wp term create fodmap_type High --description="Food's FODMAP high Lactose level" --slug=high-lactose --parent=${LACTOSE}

# Mannitol
MANNITOL=$(wp term create fodmap_type Mannitol --description="Food's FODMAP Mannitol level" --slug=mannitol --porcelain)
wp term create fodmap_type Low --description="Food's FODMAP low Mannitol level" --slug=low-mannitol --parent=${MANNITOL}
wp term create fodmap_type Medium --description="Food's FODMAP medium Mannitol level" --slug=medium-mannitol --parent=${MANNITOL}
wp term create fodmap_type High --description="Food's FODMAP high Mannitol level" --slug=high-mannitol --parent=${MANNITOL}

# Sorbitol
SORBITOL=$(wp term create fodmap_type Sorbitol --description="Food's FODMAP Sorbitol level" --slug=sorbitol --porcelain)
wp term create fodmap_type Low --description="Food's FODMAP low Sorbitol level" --slug=low-sorbitol --parent=${SORBITOL}
wp term create fodmap_type Medium --description="Food's FODMAP medium Sorbitol level" --slug=medium-sorbitol --parent=${SORBITOL}
wp term create fodmap_type High --description="Food's FODMAP high Sorbitol level" --slug=high-sorbitol --parent=${SORBITOL}

# GOS
GOS=$(wp term create fodmap_type GOS --description="Food's FODMAP GOS level" --slug=gos --porcelain)
wp term create fodmap_type Low --description="Food's FODMAP low GOS level" --slug=low-gos --parent=${GOS}
wp term create fodmap_type Medium --description="Food's FODMAP medium GOS level" --slug=medium-gos --parent=${GOS}
wp term create fodmap_type High --description="Food's FODMAP high GOS level" --slug=high-gos --parent=${GOS}

There is actually now an option created in the options table showing these parent and child term relationships in the wp_options table

wp option get fodmap_type_children --format=json

Here is that output πŸ™‚

{"99":[100,101,102],"103":[104,105,106],"107":[108,109,110],"111":[112,113,114],"115":[116,117,118],"119":[120,121,122]}

It could also be fun to create a script to autocreate the commands based on the output from wp term get fodmap_type but taht is for another day.

Happy WP-CLI scripting πŸ˜‰

Sources

wp taxonomy get
wp term create