Skip Navigation

[Resolved] Is it possible to avoid duplicate value submission for any CPT field ?

This support ticket is created 7 years 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.

Sun Mon Tue Wed Thu Fri Sat
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Author
Posts
#600476

Hi I am setting up educational portal with cpt schools,colleges,courses,universities etc.There is relationship between each cpt.
My problem is a ...each school/college have a specific code registered with university or any government organization.This code nature is non duplicating.
I want to display such code on school/college page.But I can not find any option which allows or bans duplicate value submission.
Need you help ...

#600736

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

You'll need custom form validation for this using the CRED API hook cred_form_validate (https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate).

I prepared some sample code to demonstrate what is involved. I haven't tested it, but it should illustrate what is required.

/**
 * CRED custom validation to prevent duplicates
 */
add_filter( 'cred_form_validate', 'prevent_duplicate_submissions' );
function prevent_duplicate_submissions( $error_fields, $form_data ) {

	$post_type = 'cpt-slug';  // Edit
	$unique_field = 'wpcf-' . 'field-slug';  // Edit
	$form_ids = array( 101, 115 );  // Edit IDs of CRED forms

	list( $fields,$errors ) = $error_fields;

	if ( in_array($form_data['id'], $form_ids ) ) {

		// Get existing posts with unique field
		$args = array(
			'post_type'		=>	$post_type,
			'post_status'	=>	'publish',
			'numberposts'	=>	-1,
			'meta_key'		=>	$unique_field,
			'meta_value'	=>	$fields[ $unique_field ]
		);

		$matching = get_posts( $args );

		if ( !empty( $matching ) ) {

			$errors[ $unique_field ] = 'Value already used';
		}
	}

	return array($fields,$errors);
}

You would need to add this to your theme's functions.php file, or using a plugin such as Code Snippets.

Let me know how you get on.

#601606

Hi !
Thanks for your assistance.I tried this code.But its not working.Post is getting accepted with duplicate value.

#601685

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

Sorry for the delay in getting back to you, I was away for a couple of days at Christmas.

I checked the code and you are right, there is a simple omission when adding the filter.

That line needs to be like this to pass both the arguments:

add_filter( 'cred_form_validate', 'prevent_duplicate_submissions', 10, 2 );
#602266

Great ! Functionality achieved.But following error is shown...

Warning: mysqli_real_escape_string() expects parameter 2 to be string, array given in......./public_html/wp-includes/wp-db.php on line 1109

#602555

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

I'm glad that it is working, but you should not be seeing the PHP warning.

Can you first confirm that the warning only appears when you have added the above code? If you remove the code do you still see the warning?

I don't see any such warning on my test site where I have added the code.

#602570

I have confirmed by adding and removing code....

add_filter( 'cred_form_validate', 'prevent_duplicate_submissions', 10, 2 ); causes php error...

#602614

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Could I please get you to run a conflict test to see if it is possible to narrow-down the source of the warning (which I don't see on my test site).

Switch theme to twentyseventeen and test again to see if the warning is present.

Disable non-Toolset plugins and test again to see if the warning is present.

If the warning disappears it should be possible to isolate the conflict by selectively re-activating the theme or individual plugins.

Let me know how that goes so I can see how to proceed, thanks.

#602643

Hi !
Currently I have disabled all plugins expect Toolset bundle.And theme also changed to Twenty Fourteen. But still Error message appears.

#602648

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Sorry, one last thing.

Can you provide the debug info in your next reply?

https://toolset.com/faq/provide-debug-information-faster-support/

#602651

Hello !
Debug info is provided...

#602654

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

OK, that all looks in order.

There's nothing left for me to do but ask you for a copy of your site so that I can test locally.

Can you make a site duplicate for me?

https://toolset.com/faq/provide-supporters-copy-site/

If you have any problems with that I can make the duplicate myself, let me know.

#602691

I have no experience of Duplicator plugin.Plz do site duplication for me.....thanks...

#602937

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

Hi there

I took a duplicate of your site but was unable to install it successfully locally, I cannot get access to the backend.

I could try again with another plugin, All in One WP Migration.

Could you please install and activate the plugin and then let me know so that I can generate and download the archive?

#603017

Hi!
I have installed plugin you told...but its not free for multisite.So I have installed another one..wp-staging..is it fine ? let me know..