How to restrict custom field to accept duplicate value

I use register_post_typeto add an input field, say, "brand_name". I would like to deny duplicates for this field.

How can I do this in WordPress? Please help me.

Here is my snippet:

function brand_register_meta_boxes() {
if (!class_exists('RW_Meta_Box'))
        return;
    $prefix = 'post_';

    $meta_boxes[] = array(
       'title' => 'Add Brand',
        'pages' => array('brand'),

        'fields' => array(

            array(
            'name' => __( 'Brand Name', 'rwmb' ),
            'desc' => __( 'Add Brand Name', 'rwmb' ),
            'id'   => "{$prefix}title",
            'type' => 'text',
            'required' => true,

            ), 

        )
    );     
        foreach ($meta_boxes as $meta_box) {
        new RW_Meta_Box($meta_box);
    }

}
+4
source share
2 answers

It comes down to what you do on the hook save_postwhen the user field is stored in the database. It looks like you are using the RW Meta Box class. I personally have not used the RW Meta Box, but based on the Github repository at https://github.com/rilwis/meta-box/blob/master/ , you can achieve this by setting the 'multiple' => falsefield to define.

function brand_register_meta_boxes() {
    if (!class_exists('RW_Meta_Box'))
        return;
    $prefix = 'post_';

    $meta_boxes[] = array(
        'title' => 'Add Brand',
        'pages' => array('brand'),
        'fields' => array(
            array(
            'name' => __( 'Brand Name', 'rwmb' ),
            'desc' => __( 'Add Brand Name', 'rwmb' ),
            'id'   => "{$prefix}title",
            'type' => 'text',
            'required' => true,
            'multiple' => false
            ), 
        )
    );     
    foreach ($meta_boxes as $meta_box) {
        new RW_Meta_Box($meta_box);
    }
}
0

All Articles