I have a custom type typeA, a custom field fieldA belongs to typeA. Can I make the following approach?
Here is the scenario, a user create a typeA via post form, after he enter the value in fieldA, it will search fieldA in all existing typeA post. If it found the same value as user entered, show an error message and not allow it to submit the form.
add_filter('cred_form_validate','func_validate_custom_field',10,2);
function func_validate_custom_field($error_fields, $form_data){
//field data are field values and errors
list($fields,$errors)=$error_fields;
if ($form_data['id']==9999){
$args = array(
'meta_query' => array(
array('key' => 'wpcf-EMAIL-FIELD-SLUG',
'value' => $_POST['wpcf-EMAIL-FIELD-SLUG']
)),
'post_type' => 'your-post-type',
'posts_per_page' => -1
);
$posts = get_posts($args);
//check if birthday value is already on the database
if (count($posts) > 0){
//set error message for my_field
$errors['wpcf-EMAIL-FIELD-SLUG']='This E-mail already registered.';
}
}
return array($fields,$errors);
}
Where:
- Replace 9999 with your original Form ID
- Replace EMAIL-FIELD-SLUG with original field slug
- Replace your-post-type with your original post type
- Adjust the error message as required
Can I have a further question?
1. in line 15, how can I include 2 post types in the function?
2. when it shows error can it display a clickable URL link, so that user can click the link?
3. if the above point is not approachable, Can it set a hidden custom field URL to viewable, and set the value to this custom field?
2. when it shows error can it display a clickable URL link, so that user can click the link?
==>
What kind of link you want to display?
3. if the above point is not approachable, Can it set a hidden custom field URL to viewable, and set the value to this custom field?
==>
I would like to know the flow - how exactly you want to add URL and display it?