I'd like to associate new posts created under a custom post type to automatically be given a custom taxonomy term. As a heads up here are the parameters
Custom Post Type Name: business_ser_dir_ltg
Custom Taxonomy Name: secondary-listing-category
Term Name: Business Services
Term Slug: business-services
function bus_default_terms( $post_id, $post, $update ){
if ( 'business_ser_dir_ltg' == $post->post_type) {
$default_term = '5480'; // ID of the required term
$taxonomy = "business-services"; // slug of the taxonomy
wp_set_post_terms( $post_id, $default_term, $taxonomy );
}
}
add_action( 'save_post', 'bus_default_terms', 30, 3 );
This is the code I created, but when I create a new post it does not automatically assign the term Business Services to the newly created post.
$taxonomy
, it's the slug of the taxonomy (secondary-listing-category
) and not the slug of the term. look the documentation here : developer.wordpress.org/reference/functions/wp_set_post_terms