Skip Navigation

[Resolved] Force unique field on CRED post

This thread is resolved. Here is a description of the problem and solution.

Problem:

When user submits the CRED form for creating post, I need the field wpcf-anuncio-gratuito to be unique.

Solution:

You can get the field "my_field" value submitted by user:
$fields['wpcf-anuncio-gratuito']['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

Relevant Documentation:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

This support ticket is created 6 years, 8 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Hong_Kong (GMT+08:00)

This topic contains 8 replies, has 2 voices.

Last updated by Luo Yang 6 years, 8 months ago.

Assisted by: Luo Yang.

Author
Posts
#620237

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?

#620324

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

#620733

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

#620789

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

#621201

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

#621203

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

#621220

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

#621585

Dear luo,

It worked perfectly! Thank you very much!

#621915

You are welcome