I am trying to create a couple of custom posts and set a custom taxonomy term to it on plugin activation. I am using the register plugin activation hook to do it. The posts are created properly, but I the post term is not being set. I am using wp_set_object_terms to do it. What could be the possible cause?

My code resembles this:

register_activation_hook( __FILE__, 'insert_latest_books' );

public function insert_latest_books() {
    //Get the latest books
    $data = new Data_Fetcher();
    $latest_books = $data->get_latest_books();

    foreach($latest_books as $latest_book) {
        //Create a book post for each latest book
        $book = [
            'post_type' => 'book',
            'post_status' => 'publish',
            'post_title' => $latest_book->name,
            'post_content' => $latest_book->description

        $book_id = wp_insert_post($book);

        //TODO: Not working on plugin activation
        wp_set_object_terms($book_id, array('latest'), 'label');
        set_book_thumbnail($book_id, $latest_book->name, $latest_book->preview_image_url);
        update_post_meta($book_id, 'book_code', $latest_book->embed_code);
        update_post_meta($book_id, 'book_pages', $latest_book->length);
  • 2
    Your issue is that you are trying to insert terms before your taxonomy is registered. Have a look at my answer to something similar here – Pieter Goosen Aug 27 '15 at 10:25
  • 1
    Thank you! Why doesn't it work just like register_post_type works on the init action hook? – Rutwick Gangurde Aug 27 '15 at 10:35
  • 2
    It still works the same, but the issue here is that by the time init fires, your plugin has already registered and done what you asked it to do. What I have done in my code is to only insert the terms ones the init hook has fired to register the taxonomy, the taxonomy still gets registered on the init hook – Pieter Goosen Aug 27 '15 at 10:47
  • 1
    @PieterGoosen Great! I read your answer thoroughly and realized you're registering the taxonomy on plugin activation and hooking it to the init action as suggested in the codex. Did the same and it worked. Please add an answer so that I can accept it! – Rutwick Gangurde Aug 28 '15 at 10:34
  • Just upvote my answer in the link and post whatevert you have as an answer. Unfortunately I don't have the time to post a complete answer ;-) – Pieter Goosen Aug 28 '15 at 11:58

register_activation_hook is one-time only activation hook. It can be used for database structure creation, as example, that needs to fire only on plugin activation trigger.

In order to set a custom taxonomy you need a hook that will fire not just once, but per every WordPress load. Such as init.

Use add_action( 'init', 'YOUR_CALLBACK' ); to achieve that

| improve this answer | |
  • If you read my question description, I am using the init hook to create the taxonomy. I am implementing the solution by @Pieter Goosen. – Rutwick Gangurde Aug 27 '15 at 12:13
  • You mean in the comments? Confirmed. the Pieter answer is more descriptive + shows how to register taxonomy on both init and withing registration. – Ignat B. Aug 27 '15 at 12:19
  • 1
    Yes @Ignat.B! I should have read your comment earlier, it would have saved me some time in understanding the code. Thanks! Voted up. – Rutwick Gangurde Aug 28 '15 at 10:35

I learned from the comments by @Pieter Goosen and @Ignat.B, and @Pieter Goosen's answer here, that I need to register the taxonomy on plugin activation, and insert the term. Then I need to hook the taxonomy registration code to the init action hook as suggested by the codex. This will ensure that when I insert the term on plugin activation, I have the taxonomy already registered and with init I follow WordPress's instruction to use init for that purpose.

The pseudo code:

function register_tax() {
    //Register the taxonomy

function activate() {

register_activation_hook('...', 'activate');
add_action('init', 'register_tax');

Hope this helps someone stuck in a similar situation. Pieter's answer is really helpful if you need explanation.

| improve this answer | |

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.