Support

Account

Home Forums Front-end Issues Schedule Post with Frontend Form? Reply To: Schedule Post with Frontend Form?

  • Hi,

    Thanks so much for the response. I apologize I am not very advanced with using php.

    This is what my current form looks like:

    
    <?php acf_form_head(); ?>
    
    <?php /* The loop */ ?>
    			<?php while ( have_posts() ) : the_post(); ?>
    
    				<?php 
    				
    				
    		
    				acf_form(array(
    				
    					'post_id'		=> 'new_post',
    					'post_title' => true,
    					'new_post'		=> array(
    						'post_type'		=> 'shareable',
    						'post_status'  => 'future' ,
    
    						
    					),
    					'submit_value'		=> 'Create post'
    				
    
    				)); ?>
    
    			<?php endwhile; ?>

    I am unclear on how to use the function acf-pre_save_post to set the post date variable.

    I have a datepicker field added formatted like Y-m-d. Would the below code work with my form added above <?php acf_form_head(); ?> at the top of my template?

    <?php
    
    function my_pre_save_post( $post_id ) {
    
        // check if this is to be a new post
        if( $post_id != 'new' ) {
    
            return $post_id
        }
    // The date picker field
    $postdate = get_field('schedule_post');
    
        // Create a new post
        $post = array(
            'post_status'  => 'future' ,
            'post_date'  => '$postdate' ,
            'edit_date' => 'true'
        );  
    
        // 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 );
    
    ?>