Wordpress likes to do auto-saves / auto-drafts. There is no hook currently available to stop an insert based on specific post data.
However, you can stop the post from being published based on specific post data by using wp_die(). I have used this to check for duplicate SKUs on ecommerce sites.
In this example I am getting the "SKU" value from an ACF Field, but if you're not using ACF that's fine, just use print_r($_POST);
to view your post data, and go from there.
This example also checks to see if the post we are updating is a custom post type, you could also omit that if not required.
//Check if a SKU already exists, and display an error if it does
add_action('pre_post_update', 'tix_check_for_duplicate_sku', 1, 2);
function tix_check_for_duplicate_sku( $post_id, $data ) {
//Check if we are updating a product (custom post type) if not, just return
if (get_post_type() != 'tix_product') return;
$sku = $_POST['acf']['field_662e98fccf0da'];
if ($sku != "") {
$sku_check = new WP_Query([
'post_type' => 'tix_product',
'post_status' => 'publish',
'posts_per_page' => 1,
'post__not_in' => array($post_id),
'meta_key' => 'sku',
'meta_value' => $sku,
'fields' => 'ids'
]);
if ($sku_check->found_posts >= 1) {
wp_die('Duplicate SKU: '.$sku.' exists. All SKUs must be unique! Please go back and change it...', 'Duplicate SKU', array('back_link' => true));
}
}
}