I need the same as described on https://toolset.com/forums/topic/custom-fields-to-have-unique-value/, but i don´t know how to implement it. Could you help me, please?
Hi,
As you can see, there isn't such a built-in feature within Types plugin, according to our support policy, we don't provide custom codes support:
https://toolset.com/toolset-support-policy/
As a workaround, the post ID is also an unique ID, so you don't need any custom field or custom codes to setup a unique ID for each post, you can display it with Views shortcode [wpv-post-id]:
https://toolset.com/documentation/user-guides/views-shortcodes/#wpv-post-id
Hi Luo,
I need to force a field (personal identification number) to be unique on my custom posts.
I would like to use CRED API for validation using a view to query the id field to check if it returns empty value or not.
Please, can you help me to use the API:
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
//field data are field values and errors
list($fields,$errors)=$error_fields;
//validate if specific form
if ($form_data['id']==12)
{
//check my_field value
if get_view_query_results($fields['wpcf-my_field']['value']) != ''
{
$errors['wpcf-my_field']='Field already exists';
}
}
return array($fields,$errors);
}
Thanks in advance,
Selmo
In your PHP codes, you can get the field "my_field" value submitted by user:
$fields['wpcf-my_field']['value']
Then use it to query database, check if there is any existed item
https://codex.wordpress.org/Class_Reference/WP_Query#Custom_Field_Parameters
If there is existed item, return an error
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate
Since it is a custom PHP codes problem, if you still need assistance for it, please provide a test site with same problem, and fill below private message box with login details and FTP access, also point out the problem page URL and CRED URL, where I can edit your PHP codes, I need a live website to test and debug it, thanks
Hi Luo,
I read the docs and tried the following code. The problem is that the query doesn´t work even if i am using the same field value already saved to check it.
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
list($fields,$errors)=$error_fields;
$args = array(
// custom post type slug
'post_type' => 'anuncio-gratuito',
'meta_query' => array(
array(
'key' => 'wpcf-cpf-do-solicitante-gratuito',
'value' => $fields['wpcf-cpf-do-solicitante-gratuito']['value'],
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ) {
// Error "CPF already saved" is not working
$errors['wpcf-cpf-do-solicitante-gratuito']='CPF already saved';
}
// Just a test to print the field value. The error message is showed on the form with the value that i entered (it is working).
$errors['wpcf-cpf-do-solicitante-gratuito']=$fields['wpcf-cpf-do-solicitante-gratuito']['value'];
return array($fields,$errors);
}
As I mentioned above:
https://toolset.com/forums/topic/force-unique-field-on-cred-post/#post-620789
Since it is a custom PHP codes problem, if you still need assistance for it, please provide a test site with same problem, and fill below private message box with login details and FTP access, also point out the problem page URL and CRED URL, where I can edit your PHP codes, I need a live website to test and debug it, thanks
Thanks for the details, In the settings of your CRED form "Criar conteúdo - Anúncios gratuitos 1" (ID 658), option "Status of content created or edited by this form" is "Pending Review", so you will need to query the posts in "Pending Review" status, for example, you can modify your PHP codes as below:
add_filter('cred_form_validate','my_validation',10,2);
function my_validation($error_fields, $form_data)
{
// if it is the specific CRED form
if (isset($form_data['id']) && $form_data['id']!=658) return $error_fields;
list($fields,$errors)=$error_fields;
$args = array(
// custom post type slug
'post_type' => 'anuncio-gratuito',
// post status filter
'post_status' => 'any',
'meta_query' => array(
array(
'key' => 'wpcf-cpf-do-solicitante-gratuito',
'value' => $fields['wpcf-cpf-do-solicitante-gratuito']['value'],
),
),
);
$query = new WP_Query( $args );
if( $query->have_posts() ) {
// Error "CPF already saved" is not working
$errors['wpcf-cpf-do-solicitante-gratuito']='CPF already saved';
}
return array($fields,$errors);
}
More help:
https://codex.wordpress.org/Class_Reference/WP_Query#Status_Parameters
Dear luo,
It worked perfectly! Thank you very much!