タイトルそのまま。
template_redirect アクションフック使うだけ。
タームにはカテゴリー、タグも含まれるので条件分岐はよしなに。
via. https://increment-log.com/access-redirect-404/
コード
tag_archive_template_redirect.php
<?php
add_action( 'template_redirect', 'tag_archive_template_redirect' );
function tag_archive_template_redirect() {
	if ( ! is_tag() ) { // タグアーカイブ以外は何もしない
		return;
	}
	$queried = get_queried_object();
	if ( $queried->count < 6 ) { // 総記事数が5件以下なら
		global $wp_query;
		$wp_query->set_404();
		status_header( 404 );
	}
}
- 
template_redirect: https://developer.wordpress.org/reference/hooks/template_redirect/
- 
get_queried_object(): https://developer.wordpress.org/reference/functions/get_queried_object/
- 
WP_Query::set_404(): https://developer.wordpress.org/reference/classes/wp_query/set_404/
- 
status_header(): https://developer.wordpress.org/reference/functions/status_header/
現場からは以上です。
