Support

Account

Home Forums General Issues Validate Front End Form Submission – Unique Values only Reply To: Validate Front End Form Submission – Unique Values only

  • 
    // your should only be testing the vacancy field
    // insert the field key of your field here
    add_filter('acf/validate_value/key=field_YYYYYYY', 'acf_duplicate_candidate_application', 10, 4);
    
    function acf_duplicate_candidate_application($valid, $value, $field, $input) {
      // acf now uses $_POST['_acf_post_id']
      // the other $_POST indexes may not be available on the front end
      if (!$valid || !isset($_POST['_acf_post_id'])) {
        return $valid;
      }
      $post_id = intval($_POST['_acf_post_id']);
      $post_type = get_post_type($post_id);
      
      // to get pother values submitted you must look in $_POST['acf'][$field_key]
      $candidate_email = $_POST['acf']['field_XXXXXXX']; // field key of email field
      
      $args = array(
        'post_type' => $post_type,
        'post_status' => 'publish, draft, trash', // double check possible post statuses 
        'post__not_in' => array($post_id),
        'meta_query' => array(
          'relation' => 'AND',
          array(
            'key' => 'candidate_email', // field name
            'value' => $candidate_email
          ),
          array(
            'key' => 'vacancy_applied_for', // field name
            'value' => $value
          )
        )
      );
      $query = new WP_Query($args);
      if (count($query->posts)){
        return 'You have already applied for this vacancy.';
      }
      return true;
    }