Flush rewrite rules when new cpt is registred

Post types are registered on every load. They’re not stored in the database or some other sort of persistent storage. The posts are, but not the post types.

So the correct place to flush rewrite rules is when the theme is activated. This can be done with the after_switch_theme hook:

add_action( 'after_switch_theme', 'flush_rewrite_rules' );

For a plugin, you’d do it with the activation and deactivation hooks:

register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
register_activation_hook( __FILE__, 'flush_rewrite_rules' );