How do I flush rewrite rules

I haven’t worked much with flush_rewrite_rules function, but based on the usage notes mention by nmr I think you could perhaps do something like this,

add_action( 'save_post', 'my_save_post_function' );
function my_save_post_function( $post_id ) {
  $maybe_some_extra_logic = true; // if applicable
  if ( $maybe_some_extra_logic && ! has_action( 'shutdown', 'my_flush_rewrite_rules' ) ) {
    add_action( 'shutdown', 'my_flush_rewrite_rules', 9999 ); // set suitable priority
  }
}

function my_flush_rewrite_rules() {
  flush_rewrite_rules();
}