Support

Account

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

  • I struggled to get John’s code to update the post date. It seems that if you are setting the post_status to draft or pending, then you also need another argument: “edit_date”.

    $args = array(
    	'post_status'   => 'pending',
    	'post_date'     => date( 'Y-m-d H:i:s', $date_timestamp ), // Eg: "2016-10-15 00:00:00"
    	'post_date_gmt' => date( 'Y-m-d H:i:s', $date_timestamp ), // You want this particularly when the post already exists, or the GMT date will be wrong
    	'edit_date'     => true // Must be true, prevents WordPress for "clearing" the post date for drafts and pending posts.
    );

    In addition, the pre_save_post hook MIGHT pass a post ID. To prevent the possibility of duplicate posts being added (especially if you hook in to this more than once), then you should support an existing ID. It’s easy:

    if ( $post_id ) {
    	// Update a post that was already inserted
    	$args['ID'] = $post_id;
    	wp_update_post( $args );
    }else{
    	// Insert a new post
    	$post_id = wp_insert_post( $args );
    }
    
    return $post_id;