2

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?

0

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.

0

If you are running on a multisite, you have to specify the site you are running the command for using --url so that it will load the correct theme.

For example

wp --require=check-vehicle-fallback.php vehicle-fallback export --url=yoursite.example.dev

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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