wp-cli: wp term generate xxxxx – Error: ‘xxxxx’ is not a registered taxonomy

I’m writing a WP CLI command which creates and updates taxonomies using wp_insert_term. Actions on my custom taxonomies are not accepted since they don’t show up as registered.

The wp-cli included Term_Command itself uses wp_insert_term and allows actions on default taxonomies but errors on custom taxonomies.

Various searches suggest custom taxonomies aren’t registered until init. Is there a way to run init inside wp-cli so custom taxonomies can be manipulated? Anyone got any other ideas?

2 Answers
2

I figured out a way, although it feels a bit wrong.

First test if the taxonomy exists (which it doesn’t… the first time at least), and then create it as part of the process…

if (! taxonomy_exists($this->taxonomy_manufacturer)) {
     register_taxonomy($this->taxonomy_manufacturer, 'product');
}

if (! term_exists($manufacturer['name'], $this->taxonomy_manufacturer)) {
     wp_insert_term($manufacturer['name'], $this->taxonomy_manufacturer);
}

Caveat, register_taxonomy warns of impending doom if it’s called outside of init. I’m not sure of the implications of it in my scenario, and I’ll report back if my spideysense starts tingling, but so far so good.

Leave a Comment