The below code will check if any existing posts already use the same value for a custom field submitted in a CRED form.
/** * 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); }
Let us know if this snippet is not working for you:
This snippet doesn’t work