Skip Navigation

[Resolved] Prevent duplicate records for custom types

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

Problem:
How to validate post created using CRED for duplicate entry

Solution:
You should use CRED hook "cred_form_validate" to validate the entry for duplicate post.

You can find proposed solution, in this case, with the following reply:
=> https://toolset.com/forums/topic/prevent-duplicate-records-for-custom-types/#post-697287

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

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

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10: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/Kolkata (GMT+05:30)

This topic contains 7 replies, has 2 voices.

Last updated by ujuO 6 years, 7 months ago.

Assisted by: Minesh.

Author
Posts
#696984

== Tell us what you are trying to do? ==

I want to prevent duplicate records in a custom type. Duplicates are determined via 4 fields of the custom type (a sort of pseudo composite key).

I have used the cred api to auto-generate titles for the custom type using these four fields (in both the add-new and edit forms).

I am unsure how to hook into the api to prevent duplicate records. Since I already have the title composed from the 4 fields, it should be a simple case of making an extra query to check if that title exists (or even searching based directly on comparing the 4 fields) ..

But I am not so sure .. could use an example. Also, I am sure this is bound to be a common requirement from your customs and perhaps should form a part of the base documentation.

== Is there any documentation that you are following? ==

Not particularly .. I browsed the API docs

== Is there a similar example that we can see? ==

None that I am aware of

== What is the link to your site? ==

Not applicable .. seeking general advice

#697287

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Well - CRED offers hook to validate the form. You can use CRED hook "cred_form_validate" to validate your form. Where you should check the post slug (as you said you are creating title using 4 fields so slug should be unique) - correct?

More info:
=> https://toolset.com/documentation/programmer-reference/cred-api/#cfv

Please check following related ticket that may help you - adjust the code as per your requirement:
=> https://toolset.com/forums/topic/check-posted-data-to-avoid-duplicated-entries/#post-589870

#698651

Thank you very much for responding .. this is clearer to me now.

I have a couple of questions ..

your example at https://toolset.com/forums/topic/check-posted-data-to-avoid-duplicated-entries/#post-589870 shows a matching of a form field set as timestamp

$args = array(
                    'meta_query' => array(
                        array('key' => 'wpcf-field1',
                              'value' => $_POST['wpcf-field1']['timestamp']
                            )),
                    'post_type' => 'upn',
                    'posts_per_page' => -1
                    );

--> Referring to 'timestamp' field type .. what are the types for 'single line' and select types for use as array values ?
--> If I am matching more than one field, is the following the correct syntax .. or each field should have it's own array?

$args = array(
                    'meta_query' => array(
                        array('key' => 'wpcf-field1',
                              'value' => $_POST['wpcf-field1']['timestamp'],
                              'key' => 'wpcf-field2',
                              'value' => $_POST['wpcf-field2']['select'],
                              'key' => 'wpcf-field3',
                              'value' => $_POST['wpcf-field3']['singleline']
                            )),
                    'post_type' => 'upn',
                    'posts_per_page' => -1
                    );

Thanks in advance.

#698992

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

You should try following for select and single line field. As you want to compare multiple custom fields - I've also added "relation" = And.

Please note that "meta_query" will check the postmeta (custom field) values.

$args = array(
                    'meta_query' => array(
                               'relation' => 'AND',
                        array(key' => 'wpcf-field1',
                              'value' => $_POST['wpcf-field1']['timestamp']     // for date field (if you have date field)
                            ),
                         array('key' => 'wpcf-field2',
                              'value' => $_POST['wpcf-field2']    // for select field
                            ),
                           array('key' => 'wpcf-field3',
                              'value' => $_POST['wpcf-field3']  // for single line field
                            ),
                      ),
                    'post_type' => 'upn',
                    'posts_per_page' => -1
                    );
#700037

Thanks a million, the structure is clearer to me now ..

One more question:

The examples I have seen are setting errors on form fields .. but would not serve my needs. I would rather redirect the user to a page (url) with apt message.

How can I do this ?

I have seen https://toolset.com/documentation/programmer-reference/cred-api/#cred_success_redirect but not sure if it can be called from here or how to call it ..

Thanks for your support ..

#700281

Alternatively, how can I set a general error message not tied to any form fields .. all examples, seem to be tied to form fields

#700351

Minesh
Supporter

Languages: English (English )

Timezone: Asia/Kolkata (GMT+05:30)

Well - glad to know that your original issue is resolved for validating duplicate entry.

May I kindly ask you to open a new ticket for each new question. This will help other users searching on forum. Thank you for understanding.

#701119

Thanks for all your support. I got the immediate challenge I had sorted.

Will open another ticket to see if I can get support with redirecting to a specific url.