Here is the problem I am having:
- I have a custom post type called "weddingguests"
- This custom post type "weddingguests" has a custom taxonomy, "friendsof", hierarchic
- 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:
- I have tried switching the taxonomy from hierarchic to non-hierarchic - that didn't work
- I have tried using wp_insert_term (the same code) to add terms to the post "category" - that is working
- 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
wp_insert_term
?'after_setup_theme'
to register the taxonomy, you should use a later hook to insert the terms.wp_loaded
hook is best choice for such code. However, as @s_ha_dum suggests, you should put that code inside a conditional:if ( ! term_exists( 'example-category', 'friendsof' ) )
and trigger that code (i.e. put theadd_action
) on theme switch or on plugin activation.