How to provide an option to save a post as a draft

Created by Phil Kurth, Modified on Thu, 7 Jul, 2022 at 2:30 PM by Phil Kurth

If you need to give the end user the option of controlling the post status when creating new posts using Advanced Forms Pro, you can set up a field with explicit options and use the value of this field to set the post status via the af/form/editing/post_data filter


To facilitate the example below, we've set up an additional ACF field called Publish When which allows the user to select now or later. We've used a Radio Button field type for this example. In the hooked callback, we get the value of this field and set the post status dynamically. 


<?php

add_filter( 'af/form/editing/post_data', function ( $post_data, $form, $args ) {

	// If we need to run this only on a specific form, check for the form key.
	if ( $form['key'] !== 'YOUR_FORM_KEY_HERE' ) {
		return $post_data;
	}

	// Get the selected post status from the submission.
	$when = af_get_field( 'publish_when' ) ?: 'now';

	// Set the post status.
	if ( $when === 'later' ) {
		$post_data['post_status'] = 'draft';
	} else {
		$post_data['post_status'] = 'publish';
	}

	return $post_data;
}, 10, 3 );

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article