So I solved my problem using the wp_insert_post($array)
but before I used wp_insert_post()
I had to remove a filter as shown below.
remove_filter('content_save_pre', 'wp_filter_post_kses');
remove_filter('content_filtered_save_pre', 'wp_filter_post_kses');
// Add code to insert post(s) with html elements here
$post = array(
'post_content' => '<a href="#" title="text">text</a> some random text',
);
wp_insert_post($post);
// Then add the filters back so it doesn’t change the default behavior of WP.
add_filter('content_save_pre', 'wp_filter_post_kses');
add_filter('content_filtered_save_pre', 'wp_filter_post_kses');