Support

Account

Home Forums General Issues How to make a field unique

Solved

How to make a field unique

  • So I’m working on a custom post type to work together with Advanced custom fields. And I want duplicate emails to get blocked by saying something like mail already used

    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 );
    

    I tried a solution I’ve seen of John Huebner but since I’m not really sure what I’m doing I didn’t manage to implement it properly.

    if someone could send it so it would work properly with my code above that would be amazing 😀

  • So I don’t need help anymore I got it fixed

    by adding:

    'posts_per_page' => -1,
    to this

    $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);
    
  • So I don’t need help anymore I got it fixed

    by adding:

    'posts_per_page' => -1,
    to this

    $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);
    
Viewing 3 posts - 1 through 3 (of 3 total)

The topic ‘How to make a field unique’ is closed to new replies.