Ad

Wordpress: How To Make Unique Field With ACF And Custom Post Type

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.

Ad

Answer

I think the problem might be that you aren't querying all existing posts. If your posts per page default is configured to 5, and the 6th post turns out to have the same mail address as the updated one, your check will not find any matching posts.

Try the following: in acf_unique_value_field(), fix your query like so:

    $args = array(
        'post_type' => $post_type,
        'post_status' => 'publish, draft, trash',
        'post__not_in' => array($post_id),
        'posts_per_page' => -1,
        'meta_query' => array(
            array(
                'key' => 'email',
                'value' => $value
            )
        )
    );
    $query = new WP_Query($args);

'posts_per_page' => -1, will query an unlimited number of posts.

Also, I've seen you're using get_post_meta to retrieve ACF field values. I advise against doing so. Use ACF's get_field instead (and update_field for updating if necessary).

Ad
source: stackoverflow.com
Ad