Exclude Custom Post Type & Pages From Auto-Tag Function

In save_post action you can access to the objet of the saved post and check the post type before tagging it:

add_action( 'save_post', 'add_authors_name', 10, 2 );
function add_authors_name( $post_id, $post ) {

    // The post types you want be tagged to $valid_post_types array
    $valid_post_types = array( 'post', 'community' );

    if( in_array( $post->post_type, $valid_post_types) ) {

        $post_author = $post->post_author;  // returns the Author ID
        // get the author's WP_User object so we can get the Author Name
        $post_author_obj = get_userdata( $post_author );
        $post_author_name = $post_author_obj->first_name . ' ' . $post_author_obj->last_name;

        if( ! has_term( $post_author_name, 'post_tag', $post ) ) {

            wp_set_post_terms( $post_id, $post_author_name, 'post_tag', true );

        }

    }

}

Leave a Comment