Home › Forums › ACF PRO › ACF Form conditional status based on checkbox; Title not populated for drafts
I’m using acf_form to create posts on a page from the frontend. I’m also using acf/pre_save_post and checking a field to determine if the post should be a draft or pending status. Everything is working EXCEPT when I check the box to save the post as a draft, no title gets populated. Anyone have any ideas what I could be doing wrong? I’m using an additional field to populate the title — which works for when the status is pending, but not draft…
<?php
/**
* Template Name: Lesson Creation
*
* Page template to display the lesson creation form.
*
*/
function thad_pre_save_post( $post_id ) {
// check if this is to be a new post
if( $post_id != 'new' ) {
return $post_id;
}
// Get the Fields
$fields = get_field_objects( $post_id );
// is 'save as draft' checkbox field checked?
if (isset($_POST['acf']['field_5e679c6df913d'])) {
switch ($_POST['acf']['field_5e679c6df913d']) {
case "1":
$post_status = 'draft'; // post is correctly saved as 'draft' status
$post_title = $_POST['acf']['field_5e67ae0b70e42']; // post title field does not work
break;
case "":
$post_status = 'pending'; // post is correctly saved as 'pending' status
$post_title = $_POST['acf']['field_5e67ae0b70e42']; // post title field works
break;
}
}
// Create a new post
$post = array(
'post_status' => $post_status,
'post_title' => $post_title,
'post_type' => 'lesson',
);
// insert the post
$post_id = wp_insert_post( $post );
// return the new ID
return $post_id;
}
add_filter('acf/pre_save_post' , 'thad_pre_save_post', 10, 1 );
acf_form_head();
get_header();
require_once('template-parts/banners/banner--page.php');
?>
<section class="fullwidth-column section">
<div class="wrap">
<?php while ( have_posts() ) : the_post(); ?>
<?php get_template_part( 'template-parts/wp-content/content', 'page' ); ?>
<?php endwhile; ?>
<form id="post" class="acf-form" action="" method="post">
<?php
acf_form(array(
'form' => false,
'id' => 'new-lesson',
'post_id' => 'new',
'post_content'=> false,
'field_groups' => array(139),
));
?>
<?php
acf_form(array(
'form' => false,
'id' => 'new-lesson',
'post_id' => 'new',
'post_content'=> false,
'field_groups' => array(82),
));
?>
<?php
acf_form(array(
'form' => false,
'id' => 'new-lesson',
'post_id' => 'new',
'post_content'=> false,
'field_groups' => array(84),
));
?>
<?php
acf_form(array(
'form' => false,
'id' => 'new-lesson',
'post_id' => 'new',
'post_content'=> false,
'field_groups' => array(86),
));
?>
<div class="acf-form-submit">
<input type="submit" class="acf-button button button-primary button-large" value="Submit or Save Lesson">
<span class="acf-spinner"></span>
</div>
</form>
</div>
</section>
<?php get_footer(); ?>
I was able to solve my own problem…
Rather than adding the title like I did above, I modified the array to create the post:
'post_title' => $_POST['acf']['field_5e67ae0b70e42'],
The topic ‘ACF Form conditional status based on checkbox; Title not populated for drafts’ 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.