I have two taxonomies being registered in a functions file. They are then initialized like so:

function foo_register_post_types_and_taxonomies() {
  foo_register_post_types(); // defined in lib/post_types.php
  foo_register_taxonomies(); // defined in lib/taxonomies.php

add_action('init', 'foo_register_post_types_and_taxonomies');

The problem is that within a plugin i'm building, those two taxonomies are invalid. I think from what I've been reading is that the plugin runs before the taxonomies have been initialized? How do solve this so I can access these tax's from within my plugin?

In other words, outside of the plugin in any template file, when I run get_terms('school'), I get an array of terms for the taxonomy 'school'. Running this same code within a plugin returns invalid taxonomy. That's my problem in a nutshell.

  • You haven't really provided enough information. What does "within" the plugin mean? What are you trying to do? – s_ha_dum Dec 25 '15 at 5:18
  • @s_ha_dum, I added a little more to the issue. Hope this helps you understand what's going on. Thanks! – Matt Kaye Dec 25 '15 at 13:18
  • where in your plugin do you try to use get_terms? you need to hook it to run later than init, after the taxonomies are registered. everything you do in a plugin should be hooked to run on some action or filter, typically nothing should just run when the plugin file is loaded. – Milo Dec 25 '15 at 15:32

Your taxonomies register where you've told them to register, on the init hook which is the recommended hook for registering them. That much you are doing correctly.

And yes, the plugins themselves will load prior to that hook's execution. You can see that by comparing the init hook's location in the load sequence to the plugins_loaded hook's placement.

You don't say exactly what you are trying to accomplish but the trick is that any code intended to access your taxonomies needs to run on the init hook (possibly with a higher than normal priority) or on a later hook.

  • Thanks. Wrapping my plugin in a function and adding and init hook with a later execution order solved this for me. For others looking for an answer, My taxonomies registered outside the plugin: add_action('init', 'foo_register_post_types_and_taxonomies', 10); My code that needs to use those taxonomies: add_action('init', 'foo_plugin_code', 20); – Matt Kaye Dec 25 '15 at 16:59

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.