Support

Account

Home Forums ACF PRO ACF Form conditional status based on checkbox; Title not populated for drafts

Solved

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'],

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.