You could consider using the wp_insert_post_empty_content
hook. This hook determines if a post is considered empty and if so, prevents it from being saved. For example:
/**
* Checks whether some post types have a topic.
*
* @param bool $maybe_empty Whether the post should be considered "empty".
* @param array $postarr Array of post data.
* @return bool Whether the post is empty.
*/
function my_plugin_require_topic( $maybe_empty, $postarr ) {
if ( ! $maybe_empty && in_array( $postarr['post_type'], array( 'article', 'event', 'resource' ), true ) {
$maybe_empty = empty( $postarr['tax_input']['topic'] );
}
return $maybe_empty;
}
add_filter( 'wp_insert_post_empty_content', 'my_plugin_require_topic', 10, 2 );
For how to implement the custom error message, that would depend on where you want the error message surfaced. This is because there are multiple ways that a post could be edited, and surfacing the error message would depend on the interface you’d want to target.