Support

Account

Forum Replies Created

  • I’m not sure why this validation is not working. Could I get some direction what it might be?

  • Ok, I got the wp_insert_post to work with this:

    function my_pre_save_post( $post_id ) {
    
        // check if this is to be a new post
        if( $post_id != 'new_event' ) {
    
            return $post_id;
    
        }
    
        $start_time = $_POST['acf']['field_58c2cb3c85ad0'];
        $date_key   = strtotime($start_time);
        $coack_key  = 'coach';
        $coach_object = $_POST['acf']['field_59066c838afa6']; // this works
    
        // Create a new post
        $post = array(
            'post_status'  => 'publish' ,
            'post_type'    => 'event' ,
            'post_title'   => 'Video Meeting',
            'meta_input'    => array(
              'event_key' => $date_key.$coach_object
              )
        );  
    
        // insert the post
        $post_id = wp_insert_post( $post ); 
    
        // return the new ID
        return $post_id;
    
    }
    
    add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );

    And it works great. But I’m having trouble with the validation args. I’m not sure how to test the post value against meta input for all the events.

    Here is my validation code:

    function acf_unique_value_field($valid, $value, $field, $input) {
        if (!$valid || (!isset($_POST['post_ID']) && !isset($_POST['post_id']))) {
          return $valid;
        }
        if (isset($_POST['post_ID'])) {
          $post_id = intval($_POST['post_ID']);
        } else {
          $post_id = intval($_POST['post_id']);
        }
        if (!$post_id) {
          return $valid;
        }
        $post_type = get_post_type($post_id);
        $field_name = $field['name'];
        $args = array(
          'post_type' => $post_type,
          'post_status' => 'publish, draft',
          'post__not_in' => array($post_id),
          'meta_query' => array(
            array(
              'key' => $field_name,
              'value' => $value
            )
          )
        );
        $query = new WP_Query($args);
        if (count($query->posts)){
          return 'This Value is not Unique. Please enter a unique '.$field['label'];
        }
        return true;
      }
      add_filter('acf/validate_value/name=event_key', 'acf_unique_value_field', 1, 4);

    Thanks!

  • John, thanks for the new direction. I’m working on it and here is my code. What I thought I would do is create “key” for each event based on the event date converted to a string along with the coach ID. And that is what I’m trying to validate against.

    I have code adding that key here..

    function my_post_title_updater( $post_id ) {
    
      if ( get_post_type( $post_id ) == 'event' ) {
    
        $my_post = array();
        $my_post['ID'] = $post_id;
        $start_time = get_field('start_time', $post_id);
        $curent_user_id = get_current_user_id();
        $coach_key = 'coach';
        $coach_object = get_field_object($coach_key, $post_id);
        $coach = $coach_object['value']->post_title;
        $date_key = strtotime($start_time);
        $event_key = 'event_key';
    
        $my_post['post_title'] = 'Video Meeting' . ' @ ' . $start_time . ' with ' . $coach;
        update_field( $event_key, $date_key, $post_id );
        
        wp_update_post( $my_post );
      }
    }
     
    // run after ACF saves the $_POST['fields'] data
    add_action('acf/save_post', 'my_post_title_updater', 2);

    It works great. But I think I have this firing AFTER the post is saved. In my mind, the new post can’t validate against this key because it’s not entered until after the post is saved.

    I don’t know how to add it BEFORE.

    And here is my code for the validation.

    add_filter('acf/validate_value/key=field_590810149f20f', 'my_acf_validate_value', 10, 4);
    
    function my_acf_validate_value( $valid, $value, $field, $input ){
      
      // bail early if value is already invalid
      if( !$valid ) {
        
        return $valid;
        
      }
      
      $event_key = $value;
    
      if ( $value == '1491350400') :
        
        $valid = 'This slot is taken, please select a different time';
        
      endif;
      
      
      // return
      return $valid;
      
      

    Thanks again for all your help!

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