This issue shouldn’t happen if you’re using the classic editor, however, if you’re using the block/Gutenberg editor which uses the REST API, then that issue can be fixed by using the wp_after_insert_post
hook instead.
Excerpt from https://make.wordpress.org/core/2020/11/20/new-action-wp_after_insert_post-in-wordpress-5-6/:
The new action
wp_after_insert_post
has been added to WordPress 5.6
to allow theme and plugin developers to run custom code after a post
and its terms and meta data have been updated.The
save_post
and related actions have commonly been used for this
purpose but these hooks can fire before terms and meta data are
updated outside of the classic editor. (For example in the REST API,
via the block editor, within the Customizer and when an auto-draft is
created.)
So try using wp_after_insert_post
:
add_action( 'wp_after_insert_post', 'get_the_terms', 10, 3 );