I recently ran into a similar issue (rather tackling how to reset slugs that are dirty after importing). This is a snippit of the code I used to reset the slugs:
$wpdb->update(
$wpdb->posts,
array( 'post_name' => sanitize_title( $post_to_fix->post_title ) ),
array( 'ID' => $post_to_fix->ID ),
array( '%s' ),
array( '%d' )
);
Primarily focus on sanitize_title
, where it sanitizes the post title and then saves to the post_name. I believe in your case if you were to query against the post_name instead of title you would have a better chance of handling nuances in the title and providing an accurate match. So here’s a rough pass at what you might look at adding into your insert post check/add logic:
// check & set from a valid $_POST property or random string for a post name
$post_name = !empty($_POST['post_title']) ? generateRandomString() : $_POST['post_title'];
// filter by post type, note it can also take an array of post types
$post_type="post";
$get_post_args = array(
'name' => $post_name,
'post_type' => $post_type,
'post_status' => 'any',
// get all posts
'posts_per_page' => -1,
// improve the performance of your query
'fields' => 'ids',
'no_found_rows' => true,
'update_post_term_cache' => false,
'update_post_meta_cache' => false
);
$dup_posts_check = new WP_Query( $get_post_args );
if( !empty($dup_posts_check) ){
echo '<p style="color:#fff">Sorry, That Title already exists!</p>';
} else {
//insert the post
}