Controlling post status in front end dashboard
Support › MB Frontend Submission › Controlling post status in front end dashboardResolved
- This topic has 4 replies, 3 voices, and was last updated 4 years ago by
Kara.
-
AuthorPosts
-
April 19, 2021 at 2:21 AM #27360
Fanomenal
ParticipantIs 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?
April 19, 2021 at 10:01 AM #27370Long Nguyen
ModeratorHi,
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/
November 4, 2021 at 2:40 AM #31721Kara
ParticipantHi 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!
November 4, 2021 at 12:50 PM #31734Long Nguyen
ModeratorHi 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 );November 4, 2021 at 8:23 PM #31737Kara
ParticipantThanks 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 🙂
-
AuthorPosts
- You must be logged in to reply to this topic.