Skip Navigation

[Resolved] Pausing post submission to ensure no duplicates

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/Karachi (GMT+05:00)

This topic contains 6 replies, has 2 voices.

Last updated by simchaH 1 year, 11 months ago.

Assisted by: Waqar.

Author
Posts
#2360009

Hi,

I have posts that are available to all guests on the frontend to submit new info to the database. I realized that if a guest does not first check the main view that is displaying all posts beforehand, they may come to submit a post that is already in the database and that would result in duplicates. (Same issue for admin in the backend). I am wondering if there is a way for Toolset to search the db upon "submit", and if it finds a duplicate based on the post title, then it gives an error message saying that the post already exists? - I didn't see any such setting built in, I am wondering if it is possible to be coded, if I can get pointed in the right direction.

As an alternative workaround, I was thinking of having the user first search the view that contains all the posts to see if there are duplicates. Is it possible to have the same view in multiple locations/pages on my website? If yes, then I can try to paste it on top of the post form.

Thanks,
Sim

#2360417

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Hi Sim,

Thank you for contacting us and I'd be happy to assist.

To add a form validation so that the post with the same title can't be created, the "cred_form_validate" hook can be used:
https://toolset.com/documentation/programmer-reference/cred-api/#cred_form_validate

For example:


add_filter('cred_form_validate','func_validate_title',10,2);
function func_validate_title($error_fields, $form_data){
	
	list($fields,$errors)=$error_fields;
	// list of form IDs to apply this validation
	$forms_array =  array( 12345 );
	// if the current form is one of the target forms
	if (in_array($form_data['id'], $forms_array)) {
		// get the title from the form's title field
		$title = $_POST['post_title'];
		// query to get the posts to compare
		$args = array( "post_type" => "book", "posts_per_page" => -1, "post_status" => array('publish') );
		$query = get_posts( $args );
		//set error message for the title field if a post with the same title exists
		if (count($query) > 0){
			foreach ($query as $post) {
				if (strtolower($post->post_title) == strtolower($title)) {
					$errors['post_title']='Post with this title already exists!';
					break; 
				}
			}
		}
	}

	//return result
	return array($fields,$errors);
}

Notes:
1. You'll replace "12345" with the ID of the target form.

2. You'll replace "book" with the actual slug of the post type that you're adding through this form.

Additionally, you can also include a text note on top of your form, to first use the search form (with a link to the page with a search view) and only add a new post, if it doesn't exist already.

regards,
Waqar

#2360871

Waqar, you're amazing! that worked like a charm. I just had a quick follow up question- if I wanted to validate this across 3 separate forms, will the array with the post ID accept multiple ID's? Can I add this for example and it won't break? :

$forms_array = array( 12345, 6789, 15904 );

Thanks,
Sim

#2361477

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

Thanks for the update and glad that it worked.

Your understanding is correct and you can pass multiple form IDs in the array "$forms_array".

But, be careful to use it only for the forms where the post type being added/edited is the same.
( because in the "$args" array, we're cycling to get the titles from only a particular post type )

#2361707

Got it, thanks so much!

One more question I had - is there something just like this but instead of for the frontend form, for the backend admin? So when an admin wants to click "submit" on a new post it'll check to see if there is one already created?

Thanks,
Sim

#2361735

Waqar
Supporter

Languages: English (English )

Timezone: Asia/Karachi (GMT+05:00)

You're very welcome!

Applying a validation like this in the admin area's post edit screen will be a little more complicated through code. But, you'll find a number of articles online that share plugins and custom code to achieve this:

hidden link
hidden link
hidden link

#2365441

My issue is resolved now. Thank you Waqar!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.