Here is the problem I am having:

  1. I have a custom post type called “weddingguests”
  2. This custom post type “weddingguests” has a custom taxonomy, “friendsof”, hierarchic
  3. I want to programmatically insert into this custom taxonomy “friendsof” 2 terms: “Friends of the Bride” and “Friends of the Groom”

Here is the function and the action I am using to insert one term:

// programatically add 2 terms to the taxonomy "FRIENDS OF"
function example_insert_category() {
    wp_insert_term(
        'Example Category',
        'friendsof'
    );
}
add_action( 'after_setup_theme', 'example_insert_category' );

The problem: wp_insert_term doesn’t appear to be working with my custom taxonomy

What I have tried:

  1. I have tried switching the taxonomy from hierarchic to non-hierarchic – that didn’t work
  2. I have tried using wp_insert_term (the same code) to add terms to the post “category” – that is working
  3. I have tried assigning the custom taxonomy “friendsof” to the posts and then add my term ( I thought there is a problem with the way I am building my custom post types) – that didn’t work

2 Answers
2

try init instead of after_setup_theme

function example_insert_category() {
    wp_insert_term(
        'Example Category',
        'friendsof'
    );
}

add_action( 'init', 'example_insert_category' );

Leave a Reply

Your email address will not be published. Required fields are marked *