Support

Account

Home Forums Front-end Issues Front-End Form Save as Custom Status

Solved

Front-End Form Save as Custom Status

  • Hello,

    I’m working on a custom site which has a CPT for submitting a program for a conference. The form is set to save to draft by default, unless the user checks a box acknowledging they are submitting the form. I have the pre_save_post filter below–I’ve tested that the value $status is updating correctly based on the checkbox’s value (checked for submitted, unchecked for draft), but the form either always submits as a draft or as submitted. Any ideas?

    	add_filter( 'acf/pre_save_post', 'merge_pre_save_post', 10, 1 );
    	function merge_pre_save_post( $post_id ) {
    		
    		if( is_admin() ) :
    			return;
    		endif;
    		
    		//Set the status based on the form
    		if( $_POST['acf']['submitted'] == 'submitted' ) : $status = 'submitted'; else : $status = 'draft'; endif;
    		
    		//Check if it's not a new post
    		if( $post_id != 'new_post' ) : 
    			wp_update_post( array(
    				'post_id'		=>	$post_id,
    				'post_status'	=>	$status,
    			) );
    		
    		//
    		else :
    			$post_id = wp_insert_post( array(
    				'post_status'	=>	$status,
    			) );
    		
    		endif;
    		
    		return $post_id;
    	}
  • The one thing that I can see wrong is that this $_POST['acf']['submitted'] has no value. ACF fields are submitted using the field key and not the field name, for example $_POST['acf']['field_abc123']. So you’ll need to substitute your field key there to get any type of a result. More than likely your currently generating an undefined index error in php. There is more information on this here https://www.advancedcustomfields.com/resources/acf-save_post/ where it talks about using this other filter before ACF has saved values.

  • Hi John,

    Thank you for your reply–I have the checkbox as an added field outside of the actual ACF form. I did test the function (see revised function below to see how I tested) for a scenario with the checkbox both checked and unchecked. In each case, the variable $status is being set correctly. But, it’s not being set correctly on save.

    The form:

    <form id="new-program" class="new-program" action method="post">
    				<?php acf_form( array(
    					'post_id'				=>	'new_post',
    					'new_post'				=>	array(
    													'post_type'		=>	'program',
    					),
    					'submit_value'			=>	'Submit Program',
    					'id'					=>	'new-program',
    					'post_title'			=>	true,
    					'post_content'			=>	false,
    					'form'					=>	false,
    					'return'				=>	$return_url,
    					'label_placement'		=>	'top',
    					'instruction_placement'	=>	'label',
    					'uploader'				=>	'basic',
    					'field_el'				=>	'ul',
    					
    					'fields'				=>	array( 
    													'program-submission_blind_description', 
    													'program-submission_blind_abstract', 
    													'program-submission_blind_learneroutcomes',
    					),
    				)) ; ?>
    				
    				<input type="checkbox" id="submitted" name="submitted" value="submitted" />
    				<label for="submitted" id="label-submitted">
    					I confirm that my submission is complete and ready for review. I understand that once I submit this program, I cannot edit it.
    				</label>
    
    				<button type="submit">Save Draft</button>
    			</form>

    The filter:

    add_filter( 'acf/pre_save_post', 'merge_pre_save_post', 10, 1 );
    	function merge_pre_save_post( $post_id ) {
    		
    		if( is_admin() ) :
    			return;
    		endif;
    		
    		//Set the status based on the form
    		if( $_POST['acf']['submitted'] == 'submitted' ) : $status = 'submitted'; else : $status = 'draft'; endif;
    		
    		//FOR TEST ONLY
    		print_r( $_POST );
    		echo $status;
    		exit;
    		
    		//Check if it's not a new post
    		if( $post_id != 'new_post' ) : 
    			wp_update_post( array(
    				'post_id'		=>	$post_id,
    				'post_status'	=>	$status,
    			) );
    		
    		//
    		else :
    			$post_id = wp_insert_post( array(
    				'post_status'	=>	$status,
    			) );
    		
    		endif;
    		
    		return $post_id;
    	}

    In both cases–checked and unchecked–the value for $status is correct; it’s just not being applied to the post correctly.

  • I can’t really make out all of the code you posted without the code tags, but I did notice this

    
    <input type=”checkbox” id=”submitted” name=”submitted” value=”submitted” />
    

    this would be in $_POST['submitted'] and not $_POST['acf']['submitted']

  • I updated the $_POST['submitted']. Using the filter, if I submit the form with “submitted” unchecked (you can see where $status was output using echo and it show ‘draft’ as intended):

    Array
    (
        [_acfnonce] => 
        [_acfchanged] => 1
        [_acfform] => a1hNb1hBTmg2Z0J1V3BDbTdpbm5qeDVNcVNUOEI0OElsb05hMmtDaFpBdlFOS29HaURtZFZkVTJrQ0JlUGFNRms4bkE3K0Q5M3NHeFBodTdCRmFjdVczbEFPZ0pnOFNOcmdsempQaDdBS2NVUTJEbitXQldpM2JtQmpZMnVGdC83NHZoNFNYNjE0dzlteXFDb1ZSdVBiVkE5Uk4zZ0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1EZUNDa1EvZktLTlpwVEdQLzlFb1lWRDh5SnVHVzVPb25qZTlEZEtGZHRlVUtFRGRhQUFnditKSzJOUmpscWtxeDQxVlFhWkdGTFFHQk96aFlXemc0NWFnd0IyeW95V2JqM28rQ1RIUFl1QXJkdEtFRG56OVRPMGEyUVhlSlJNSno5RG1KNnBhMEN6VGtzTS9SYmhCeEYvTkZzU2xxdUh4ZkdFaTAvdk9aZTZveHBVWXlPRXEwbmRha01qL3MybUs1ZUVZYTZYczAzS21vLy9EamNTVmoyUmVnT091eUlWWHFSVjYxd2RRSms5YmJ3Z1JndWIzbHQyNUUyYmNvZTlNYXZpcmsyQ0NVUk5VK0xrc2JvblNaL3dqWlNIVHB4Z2twWWlmMjNOQ2c9PTo6owsszaga9XIEzYCy+P8j9Q==
        [acf] => Array
            (
                [field_58dff18e13f03] => Test
                [field_58dff21213f05] => Test
                [field_58dff27513f08] => Test
                [_validate_email] => 
            )
    
    )
    draft

    With the checkbox checked you can see $status outputs ‘submitted’ as intended:

    Array
    (
        [_acfnonce] => 
        [_acfchanged] => 1
        [_acfform] => ME1VRWtzdzJLVVo5TXBmZWphZFJMNkJ4T0JobzZpNFZlOFp2NDNkUi9SWUtjYmhFblExQkxSem5WaHVSNnZ1UFhQQlZML0xpYW1vQkZWZ1Rnb3k5ZGtIYUVLYlJvc2NOQ3hCbldqd0NMTVgvNkdEUjg4NnU3emlyZXJlN2hoRjlmVkplNy8rR0xNRXZUdHh5YVN0a081K1oyWDUyOFRJcFZBeFVIN2ZUaFhHTjY1UG42WWdaSUxrZzhyTXFTTTJuN1FDQklMTERCbVIySjM3R3hXS0RzakU2MnJLZkNQWXpONjYxT3prdXNRQ0RnT2xaZGVjSWdwMDFIYUxWTlExNkxaaHZLZkZoUUt5MzBybWs4MzczaUtMZFlmMXlKNkMzaVhnempSS1RCZmozZXE0Z0lPUHpTU3lWVkRIWFZ1SnhMTldJZHRtbDA1Y245NDNjQm9Mei85TVZsbjVoS2xvaVRUaFYxem9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2haSXZLd2doaitvcTRKWGRKSVE4TlNJSkNqQTRIdEM2Y0Nzd3Q0Y3g5cytNTENIR0NldEtaNFlZeW1nekVFWGtraXRaWk0vbTBKMlBMSmxpdmNUbnF3Z2c9PTo6/ovmIPCQDeflxg6kNBOk6Q==
        [acf] => Array
            (
                [field_58dff18e13f03] => Test
                [field_58dff21213f05] => Test
                [field_58dff27513f08] => Test
                [_validate_email] => 
            )
    
        [submitted] => submitted
    )
    submitted
  • Does that mean you got it worked out? Sorry, it’s not clear.

    This is the way a checkbox works, it’s not submitted in $_POST when it’s not checked. You need to use something like if (isset($_POST['submitted'])) first to see if there is any value.

  • I am getting the correct $status value from the form. However, the 'post_status' => $status is not being correctly passed from the filter acf/pre_post_save as shown in the function above. Regardless of the value of $status, it still saves the post as published. I’ve even left the post_status definition in the acf_form() declaration undefined and blank just to see.

    The value of $status is not being applied correctly.

  • I honestly don’t know. I spend some time looking at the ACF code and I can’t find a reason that this would be happening.

    I can tell you this, since you are using a priority of 10 for your filter and you are using new_post as your post ID then the built in acf filter is running first, it has a priority of 2. The default post status set by acf should be draft.

    Additionally, since the acf filter is running first the post ID will never be new_post when your filter is run. The wp_insert_post() portion of your code should never be run.

    The only conclusion that I can come up with is that there is some other filter running after yours that is altering the post status again to publish.

  • Well, I figured it out–I incorrectly set the args for the wp_update_post(), using 'post_id' when I should have used 'ID' to define the argument for the post’s ID. Correcting this had rendered the function to work correctly.

    Thank you for all your help, John!

  • I’m glad you figured it out and sorry I wasn’t any help. I usually catch stuff like that too and I never ever noticed it. Believe it or not I actually make the same mistake all the time.

  • Sometimes you just have to “say things out loud” to realize things like this–or in our case, post them to the support forum. Thanks for your help! It was causing me great agony in trying to test the custom system.

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

The topic ‘Front-End Form Save as Custom Status’ is closed to new replies.