Support

Account

Home Forums Front-end Issues Check for duplicate post based on custom date field Reply To: Check for duplicate post based on custom date field

  • 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!