I`m trying to retrieve the names of taxonomy items and include them into a theme admin panel.

function retrieve_my_terms() {

    global $terms;

    $terms = get_terms('taxonomy');

    foreach ($terms as $term) {
        $option = $term->name;
        return $option;

The function is added after the functions which created the custom post type and taxonomy.

From what I've found out, it seems that the init action occurs after the theme's functions.php file has been included, so if I'm looking for terms directly in the functions file, I`m doing so before they've actually been registered.

In other words, init action doesn't fire until after the theme functions file is included, therefore any term retrieval must occur after init.

My problem is that I don`t know how to retrieve the terms after the init.

Any answer will be much appreciated!

Thank you! Madalin

  • Hi @Madalin - can you post some code and explain precisely what you're trying to achieve as this will better help us answer the question. Nov 21, 2012 at 12:21

3 Answers 3


You can add the action on the init itself, just increase the priority of the add_action call. The higher the priority the later the function is called.

add_action('init', 'retrieve_my_terms', 9999);

But my suggestion is that you should do these kind of things as late as possible, preferably just before the first time they are used. There's an action 'wp_loaded' which gets called after full wordpress has been loaded & ready to start working on the output. That action might work for you.

  • Given the code posted in the question, this would not do anything, it would grab the terms in a taxonomy then return the slug, but nothing is done with it, its not stored for future use
    – Tom J Nowell
    Nov 21, 2012 at 12:30
  • actually it's being retrieved in a global variable Nov 21, 2012 at 13:05

It's true that functions.php is loaded before init - and that most post types will be registered using that hook. But there are later hooks that you can use - and which will be more appropriate.

You say you need access the terms inside functions.php. But when do you need them? init fires on every page load - front and back - and I don't think you mean to retrieve the terms on every page load. There are lots of hooks available to you and they're all triggered in various contexts. If you tell us precisely what you want to do with the terms that will determine which hook you should use.

(I'm a bit confused by include them into a theme admin panel). It sounds like you might be trying to output a list in an admin metabox. Regardless, can you include some code?

  • I want to get the categories of a custom post type and add them inside a meta box which will be shown only if a page is using a certain template. For example, I create a page called Portfolio, assign to it the "Portfolio" template. A metabox with categories of the portfolio custom post type will be shown. Heres the function: http://pastebin.com/yEe3Jwxw ; Im using Ohad Raz`s meta box class to generate the fields:en.bainternet.info/2012/how-i-add-a-wordpress-metabox
    – Madalin
    Nov 21, 2012 at 12:31
  • In that context admin_init might be best. You should probably update your question with that context, generally the accepted answer wouldn't be much help. Nov 21, 2012 at 12:45
  • It seems that all hooks are making the function retrieve what I need to. add_action('init', 'retrieve_my_terms', 9999); is working as well. I'm guessing that what I needed to do was to just use add_action. Thank you guys for all your answers!
    – Madalin
    Nov 21, 2012 at 13:26

You can define your function in the global scope of functions.php with no issues.

It's using the function prior to registration that's the problem. Instead of doing things in functions.php in the global scope, do them in hooks. The only thing in functions.php that should be allowed to not be in a hook is the calls to add_action.

So put everything in hooks, and make your calls in a hook/action after the registration of your post types, not before in loose code in the global

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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