Check Title Unique Or Not and If not error message and dont save

i am get tired from searching 2 days for what i wanna
i have ‘area’ custom post type have Title and another field
i wanna the title of area is unique

if the user enter title ‘area1’ and this title isnt unique i wont this post published or saved in database beside make error message in the area create page

add_action('save_post_area','AfterAreaSaved',10,3);
function AfterAreaSaved($post_ID,$post,$updated){
    if ($post->post_type != 'area'){
        return;
    }
    if ($updated){
        $variantID=get_post_meta ( $post_ID, 'variantid', true );
        wp_update_term($variantID,'pa_country',array('name'=>$post->title));
    }
    else {
        $variantID = wp_insert_term($post->post_title, 'pa_country');
        if (is_wp_error($variantID)) {
            add_action('admin_notices', 'AdminDuplicateAreaError');
        } else {
            update_post_meta($post_ID, 'variantid', $variantID['term_id']);
        }
    }
}
function AdminDuplicateAreaError(){
    ?>
    <div class="acf-notice -error acf-error-message -dismiss">
    <p>This Area is Registered Before You cant Duplicate The Title</p>
    </div><?php
}

i tried save_post publish_post wp_insert_post_data
i cannt prevent post from publish
i need to prevent it like when the acf make field required when i press publish the error message appear and the post not published
how to make thing like that ?

Short Desc

i wanna check if title unique on click on publish button and if the title unique insert the post if not unique appear the message and dont save anything even as draft

0

Leave a Comment