Support

Account

Home Forums Front-end Issues Save an acf_form submission as published if field unchecked else save as a draft

Solving

Save an acf_form submission as published if field unchecked else save as a draft

  • I have a frontend form with a checkbox in it. I’d like to save new frontend submissions as published if the checkbox is unchecked. And if the checkbox is checked, I’d like to save the new submission as ‘pending review’ or ‘draft’.

    I have attempted to do this using $_POST as follows:

    add_filter('acf/pre_save_post' , 'my_pre_save_post' );
    function my_pre_save_post( $post_id ) {
    
    	// check if this is to be a new post
        if( $post_id != 'new' ) {
            return $post_id;
        };
    
        // is checkbox field checked?
        if (isset($_POST['acf']['field_5520190cde12b'] == '1') {
        	$status = 'pending';
        } else {
        	$status = 'publish';
        }
    
        // Create a new post
        $post = array(
    		'post_status' => $status,
    		'post_type'   => 'post',
    		'post_title'  => true,
        );
    
        // insert the post
        $post_id = wp_insert_post( $post );
    
        // return the new ID
        return $post_id;
    
    };
    
    acf_form_head();

    I then create the form as follows:

    acf_form(array(
       'post_id' => 'new',
       'form' => true,
       'field_groups' => array(4),
       'post_title' => true,
    ));

    However, $_POST seems to be ignored – the post saves as normal (as ‘published’).

    Any ideas what I am doing wrong?

  • I think you have an error in you if statement. Try this

    
    // is checkbox field checked?
        if (isset($_POST['acf']['field_5520190cde12b'])) {
        	$status = 'pending';
        } else {
        	$status = 'publish';
        }
    
    
  • I have almost this exact same scenario. Were you ever able to get this working?

  • will this work on acf pro?

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

The topic ‘Save an acf_form submission as published if field unchecked else save as a draft’ is closed to new replies.