Support

Account

Home Forums ACF PRO Unique Number (validate_value) back-end

Solved

Unique Number (validate_value) back-end

  • Hi Guys,

    Wondering if I could get some help.

    I’ve got a acf field called “Book number” which needs to be unique. So I’m doing a query, comparing if user post is already exists in the database, if it does then return an error message else allow to post.

    For example user enters book_number 1234, sql query return that there is already a 1234 in the database, yet it lets the user post instead of showing error.

    Would really appreciate a response.

    function my_acf_validate_value( $valid, $value, $field, $input ) 
    {
        
    	// bail early if value is already invalid
    	if( !$valid ) 
    	{	
    
    		return $valid;	
    		
    	}
    	
    	global $wpdb;
    	
    	$sql        = "SELECT DISTINCT meta_key, meta_value FROM test_postmeta WHERE meta_key = 'book_number'";
    	
    	$result     = $wpdb->get_results($sql);
    		
    	$meta_value = $result[0]->meta_value;    	
    		
    	if($_POST['book_number'] == $meta_value) 
    	{
    		
    		$valid = "Book Number already exists";
    		
    	}
    	
    	// return
    	return $valid;
    	
    } 
    
    add_filter('acf/validate_value/name=book_number', 'my_acf_validate_value', 10, 4);
Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.