I have a custom post type called 'community' and I am trying to automatically generate the tag 'community' upon publishing anything from that custom post type. In layman's terms, a kind of 'if this is a community post type, then automatically tag this post as 'community'.' I want this to apply to the community custom post type only, and not the default wordpress post type.
I have tried a few iterations of the following code (which I have cannibalized from a previous inquiry) but could not get it to work:
add_action( 'save_post', 'add_community_tag', 10, 2 );
function add_authors_name( $post_id, $post ) {
$valid_post_types = array( 'community' );
if( in_array( $post->post_type, $valid_post_types) ) {
wp_set_post_terms( 'community', 'post_tag', true );
}
}
}
I know this is a sloppy piece of coding, so I apologize for that. I have tried to google this issue but anything that remotely resembles the term 'auto tagging' returns results from plugins that automatically generate tags based on content (not based on post type).
I also think that the above bit of code is also set to apply to 'posts' as well, but I'm unsure of how to specify 'community' only. I have used the Map Cap plugin to map out the capabilities and control the 'community' type, so would I have to change the 'save-post' bit here to 'save_community'?
Sorry for all of the confusion. Thank you for your help.