Support

Account

Home Forums Backend Issues (wp-admin) Run function in acf/validate_save_post

Solved

Run function in acf/validate_save_post

  • Hello,
    I need help with acf/validate_save_post.

    I don’t know why my filter don’t run in this action.

    always done acf_add_validation_error.

    My code:

    add_action('acf/validate_save_post', [$this, 'checkEditPromoCode']);
     add_filter('check_promo_code' , [$this, 'checkCode'], 11, 1);
      public function checkEditPromoCode()
      {
        if (current_user_can('manage_options')) {
          acf_reset_validation_errors();
        }
    
        $results = apply_filters('check_promo_code', $_POST['acf']['field_5e85a3ad796cf']);
        if (!$results) return;
        if ($results[0]->post_id == $_POST['ID']) return;
    
        acf_add_validation_error( 'acf[field_5e85a3ad796cf]', 'Taki kod promocyjny już instnieje.' );
      }
    
      public function checkCode($code)
      {
        global $wpdb;
        $sql = "SELECT * FROM {$wpdb->prefix}_promocodes_meta
            WHERE meta_value = '$code'";
    
        $results = $wpdb->get_results($sql);
    
        if (empty($results)) return false;
        return $results;
      }
    
  • I used the wrong filter…

    but I have one more problem. How to get post id of saved post in this action?
    $_POST[‘ID’] – it does not work.

    add_action('acf/validate_value/key=field_5e85a3ad796cf', [$this, 'acfValidateCode'], 10, 4);
      public function acfValidateCode($valid, $value, $field, $input_name)
      {
        if( $valid !== true ) {
          return $valid;
        }
    
        $results = apply_filters('check_promo_code', $value);
    
        if ($results && $results[0]->post_id != $_POST['ID']) {
          $valid = 'Taki kod promocyjny już instnieje.';
        }
    
        return $valid;
      }
  • Okej, I’m added hidden field where i add post_id
    and i get this id – $_POST[‘acf’][‘field_5e89d88ead452’]

  • Okej, I’m added hidden field where i add post_id
    and i get this id – $_POST[‘acf’][‘field_5e89d88ead452’]

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Run function in acf/validate_save_post’ is closed to new replies.