Support

Account

Home Forums Front-end Issues How do the date of publication by a front-end form? Reply To: How do the date of publication by a front-end form?

  • Hi @buylov

    Maybe you can use something like this:

    function my_pre_save_post( $post_id ) {
    
        // check if this is to be a new post
        if( $post_id != 'new' ) {
    
            return $post_id
    
        }
        
        $posted_date = $_POST['acf']['field_1234567890'];
        $theDate = date_create_from_format('Ymd', $posted_date);
        $theDate = date_format($theDate, 'Y-m-d H:i:s');
        
        // Create a new post
        $args = array(
            'post_status'  => 'publish' ,
            'post_type'  => 'post' ,
            'post_date' => $theDate ,
        );  
    
        // insert the post
        $post_id = wp_insert_post( $args ); 
    
        // return the new ID
        return $post_id;
    
    }
    
    add_filter('acf/pre_save_post' , 'my_pre_save_post', 10, 1 );

    where field_1234567890 is your date field key. Keep in mind that I haven’t tested it yet.

    Hope this helps.