Support

Account

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

Solving

Check for duplicate post based on custom date field

  • I’m trying to build a booking “app” on a site, but I want to make sure that if there is an appointment with a given coach at a given time that acf_form won’t add the booking but will, instead, return you back to the acf_form stating that the date is already taking with the specific coach.

    function event_pre_save_post( $post_id) {
    
      if ( get_post_type( $post_id ) == 'event' ) {
    
        $start_time = get_field('start_time', $post_id);
        $post_id = $post_id->ID;
        $coach_object = get_field_object($coach_key, $post_id);
        $coach = $coach_object['value']->post_title;
    
        if ($title == ['what_do_I_put_here'] && $coach == ['what_do_I_put_here'] ) :
    
          wp_safe_redirect( esc_url( home_url() ) ); 
    
            exit; 
    
            return; 
    
           else : 
    
            return $post_id; 
    
           endif;
    
        $post_id = wp_insert_post($post);
    
        return $post_id;
      }
    
    };
    add_filter('acf/pre_save_post', 'event_pre_save_post');

    I have a feeling I’m all over the place on this, but I could sure use the help.

    Thanks!

  • I would suggest a different direction https://www.advancedcustomfields.com/resources/acf-validate_value/

    Basically, you would create a filter for both the coach and the time field.

    Both of the field values will be in the $_POST[‘acf’] array during the validation. You can get both the coach and the date/time. The you do a query of the existing posts to see if there is one that already exists with that combination. If there is then the validate fails.

    There is an example here of validating a single field to make sure it is unique https://support.advancedcustomfields.com/forums/topic/accept-only-unique-values/#post-45359 and it should not be that much of a leap to use 2 fields in the query rather than a single field.

    This would eliminate the need to redirect back to the page and make someone start over again.

    Although, I’d probably also try to include some type of list that shows when each coach is actually available since it would be quite frustrating if you have to try a dozen times. Basically the validation would be to enforce the availability information already supplied.

  • Awesome. I’m going to try this now.

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

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

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

  • from what I can see it is because you are validating $value against this $$date_key.$coach_object and this is created by doing this.

    
    $start_time = $_POST['acf']['field_58c2cb3c85ad0'];
    $date_key   = strtotime($start_time);
    $coack_key  = 'coach';
    $coach_object = $_POST['acf']['field_59066c838afa6']; // this works
    

    You must do the same thing in your validation filter and then query against this value and not $value which is passed to your filter by ACF.

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

The topic ‘Check for duplicate post based on custom date field’ is closed to new replies.