Home › Forums › Front-end Issues › Schedule Post with Frontend Form?
Hi,
I am using a front-end form to allow visitors to create a custom post type. I would like to give them the option to schedule the post rather than post it right away.
Does anyone know if scheduling posts is possible with the ACF Front-end form?
The documentation for pre_save_post is here. http://www.advancedcustomfields.com/resources/acf-pre_save_post/
You need to create a date selector field. Then in the pre_save_post function you get the value that is submitted. It will be in the array $_POST['acf']
, then you can use this value to set the post_date when creating the new post.
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 );
?>
The first thing you need to do is move acf_form_head();
to before get_header();
http://www.advancedcustomfields.com/resources/acf_form/
Then in functions.php
add_filter('acf/pre_save_post' , 'shareable_pre_save_post', 10, 1 );
function shareable_pre_save_post($post_id) {
// check if this is to be a new post
if ($post_id != 'new_shareable') {
return $post_id;
}
// The date picker field
// change field_563657f49b4d9 to the field key of your date field
$postdate = date('Y-m-d H:i:s', strtotime($_POST['acf']['field_563657f49b4d9']));
// Create a new post
$post = array(
'post_type' => 'shareable',
'post_title' => $_POST['acf']['_post_title'],
'post_status' => 'future',
'post_date' => $postdate
);
// insert the post
$post_id = wp_insert_post( $post );
// return the new ID
return $post_id;
}
Then in your template where you want to show the form
$args = array(
'post_id' => 'new_shareable',
'post_title' => true,
'field_groups' => array(3498), // change to ID of the field group to show
'submit_value' => 'Create post'
);
acf_form($args);
Thanks so much for taking the time to help me out! The above solution worked perfectly.
My only question now is, could I show a different “Submit” button conditional on whether or not a date was selected. For example if a date is selected show “Schedule” button, if not show “Create Post” button?
I’ll see if I can figure this out.
Thanks so much again for the great support.
Daniel
That’s something that I don’t know. It would take some custom javascript and I haven’t done much with javascript for ACF.
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;
I cannot manage to get this working whatsoever…. hopefully someone can assist. Perhaps things have changed in the last 8 years and there is a better way to do this?
Here’s my code to display my form:
<?php acf_form(array(
'post_id' => 'new_post',
'id' => 'new-email',
'new_post' => array(
'post_type' => 'emails',
'post_status' => 'future',
'field_groups' => array(1091),
'post_title' => true
),
'return' => '',
'submit_value' => 'Add new email'
)); ?>
Not sure if I have the field_groups ID correct or not. Not sure how to retrieve that. Is that the “post id” of the field group?
Here’s my functions file code:
add_filter('acf/pre_save_post' , 'emails_pre_save_post', 10, 1 );
function emails_pre_save_post($post_id) {
// check if this is to be a new post
if ($post_id != 'new_emails') {
return $post_id;
}
// The date picker field
// change field_563657f49b4d9 to the field key of your date field
$postdate = date('Y-m-d H:i:s', strtotime($_POST['acf']['field_emails_email_scheduled_date']));
// Create a new post
$post = array(
'post_type' => 'emails',
'post_title' => $_POST['acf']['_post_title'],
'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;
}
@infominemindmedia-com you need to match the “$post_id” in both the form and the function. It also must be something other than “new_post” because this is the default that ACF uses and runs its own filter.
So your form args should be
<?php acf_form(array(
'post_id' => 'new_emails',
'id' => 'new-email',
'new_post' => array(
'post_type' => 'emails',
'post_status' => 'future',
'field_groups' => array(1091),
'post_title' => true
),
'return' => '',
'submit_value' => 'Add new email'
)); ?>
The topic ‘Schedule Post with Frontend Form?’ is closed to new replies.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Privacy Policy. If you continue to use this site, you consent to our use of cookies.