Simply use template_redirect filter to perform check on request you get in wordpress.
add_filter('template_redirect', 'my_404_override' );
function my_404_override() {
global $wp_query;
if (!$wp_query->post) { // Check if any post is found. If not its 404.
status_header( 200 );
$wp_query->is_404=false;
// Create post object
$my_post = array(
'post_title' => 'My post',
'post_content' => 'This is my post.',
'post_status' => 'publish',
'post_author' => 1,
'post_category' => array(8,39)
);
// Insert the post into the database
$post_id = wp_insert_post( $my_post ); // Add new post in to the system
wp_redirect( get_permalink( $post_id ) ); // Finally redirect it to the post.
}
}
This will create a new post for you & also will redirect user to that post. Well, frankly speaking it’s a fix to perform the task but it serves the purpose.