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

    You still need the acf_form() in the templates and this code in the functions.php file.

    Also, you need to pass the post title and post content. It should be 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 ,
            'post_title' => $_POST['acf']['_post_title'] ,
            'post_content' => $_POST['acf']['_post_content'] ,
        );  
    
        // 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 );

    I hope this helps.