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
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
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
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 )
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
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
My issue is resolved now. Thank you Waqar!