So I'm working on a custom post type to work together with Advanced custom fields. And I have this little script that is supposed to block duplicate emails from being entered. But the script does not work as I want it to.
The code that u see below is for the custom post type page that I've linked to ACF.
function init_members() {
$labels = array(
'name' => 'Members',
'singular_name' => 'Member',
'menu_name' => 'Members',
'name_admin_bar' => 'Member',
'add_new' => 'New member',
'add_new_item' => 'New member',
'new_item' => 'New member',
'edit_item' => 'Edit member',
'all_items' => 'All members',
'search_items' => 'Search member',
'not_found' => 'No members found',
'not_found_in_trash' => 'No members found in trash'
);
$args = array(
'labels' => $labels,
'public' => true,
'exclude_from_search' => true,
'rewrite' => array('slug' => 'member'),
'has_archive' => false,
'supports' => array('title'),
'show_in_rest' => true,
'menu_icon' => 'dashicons-groups'
);
register_post_type('members', $args);
}
add_action('init', 'init_members');
function add_member_columns ( $columns ) {
unset($columns['date']);
return array_merge ( $columns, array (
'contactperson' => __ ( 'Contactperson' ),
'phone_number' => __ ( 'Phonenumber' ),
'email' => __ ( 'Email' ),
) );
}
add_filter ('manage_members_posts_columns', 'add_member_columns' );
function fill_member_columns ( $column, $post_id ) {
switch ( $column ) {
case 'contactperson':
echo get_post_meta ( $post_id, 'contactperson', true );
break;
case 'phone_number':
echo get_post_meta ( $post_id, 'phone_number', true );
break;
case 'email':
echo get_post_meta ( $post_id, 'email', true );
break;
}
}
add_action ('manage_members_posts_custom_column', 'fill_member_columns', 10, 2 );
So the script I have for trying to filter the use of duplicate emails is as follows. the source where I got it from is here
add_filter('acf/validate_value/name='.'email', 'acf_unique_value_field', 10, 4);
function acf_unique_value_field($valid, $value, $field, $input) {
if (!$valid || (!isset($_POST['post_ID']) && !isset($_POST['post_id']))) {
return $valid;
}
if (isset($_POST['post_ID'])) {
$post_id = intval($_POST['post_ID']);
} else {
$post_id = intval($_POST['post_id']);
}
if (!$post_id) {
return $valid;
}
$post_type = get_post_type($post_id);
$field_name = $field['name'];
$args = array(
'post_type' => $post_type,
'post_status' => 'publish, draft, trash',
'post__not_in' => array($post_id),
'meta_query' => array(
array(
'key' => 'email',
'value' => $value
)
)
);
$query = new WP_Query($args);
if (count($query->posts)){
return 'This Value is not Unique. Please enter a unique '.$field['label'];
}
return true;
}
I probably oversee something pretty obvious. But I'm not able to find what I'm overseeing.