Support Forum
Support › MB Frontend Submission › Controlling post status in front end dashboardResolved
Is it possible for users to switch a post to draft or publish natively.
Perhaps by creating a field on the form and writing some code in a hook like rwmb_frontend_before_process to change the post status based on the fields value?
Hi,
It is possible. You can use the function wp_update_post() to update the post status after submitting a post on the frontend.
Refer to this topic https://support.metabox.io/topic/selecting-post-formats/
Hi Long - can you help me out with the function to update the post status? Users should be able to change a status to draft or published based on the radio buttons.
Here's the code for the radio buttons.
[
'name' => __( 'Save As', 'your-text-domain' ),
'id' => $prefix . 'save_as',
'type' => 'radio',
'options' => [
'Draft' => __( 'Draft', 'your-text-domain' ),
'Publish' => __( 'Publish', 'your-text-domain' ),
],
'std' => 'Draft',
'required' => true,
'inline' => false,
],
Thanks a lot!
Hi Kara,
You should use the choice value in lower case.
[
'name' => __( 'Save As', 'your-text-domain' ),
'id' => $prefix . 'save_as',
'type' => 'radio',
'options' => [
'draft' => __( 'Draft', 'your-text-domain' ),
'publish' => __( 'Publish', 'your-text-domain' ),
],
'std' => 'draft',
'required' => true,
'inline' => false,
],
And the code to update the post status is
add_action( 'rwmb_save_as_after_save_field', function( $null = true, $field, $new, $old, $object_id ) {
$my_post = array(
'ID' => $object_id,
'post_status' => $new,
);
wp_update_post( $my_post );
}, 10, 5 );
Thanks Long - that works perfectly.
By the way - the capitalized "Draft" and "Publish" was produced when I generated the code using MetaBox AIO. I just manually changed it to use lowercase, but the code to update the post status worked for me either way.
Appreciate you help 🙂