Create front end link to save post (or unpublish post) as draft

I have created a front-end posting and dashboard system for my users to add and edit their own posts. In the dashboard area, I have a list of their posts and the following options for each post: View | Edit | Publish/Un-Publish | Delete

I have all of these links working except for the “un-publish” part. I can get a post to publish if it’s currently in draft, but once it publishes I can’t figure out how to “unpublish” it from the front end, which is a function I’d like my users to have.

For reference, I am using code from this thread to do the Publish part:
Front-End Post Submission

I’ve tried adapting it, but have not succeeded.

I also need similar functionality on the New Post form where I’d like to display a “Save Post” button along side the “Publish” button so a user can save the post a draft so as to not risk losing their work while creating the post.

Thanks for reading, and hopefully someone can help!

1 Answer
1

You can use wp_update_post() to change the status of a post.

global $current_user;
get_currentuserinfo();

$post_id = $_GET['post_id'];
$the_post = get_post( $post_id );

if ( $the_post->post_author == $current_user->ID && $the_post ) {
    $the_post->post_status="draft";
    wp_update_post( $the_post );    
}

Use wp_insert_post() with post_status => 'draft' to save a post.

Leave a Comment